SpringMVC第六篇——解決中文亂碼(POST/GET提交)
阿新 • • 發佈:2019-01-06
SpringMVC中get和post中文亂碼要分開解決:
POST提交:
只需要在web.xml中新增如下配置。post中文亂碼就解決了。
攔截規則根據自己需求
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name >encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
GET提交:
1:亂碼的地方使用進行重新編碼
String param = new String(param.getBytes("ISO8859-1"), "UTF-8");
2:在Tomcat的 服務配置檔案server.xml裡面加上配置
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
第二種是改了伺服器配置,第一種操作繁瑣。
推薦使用:
3:自定義攔截器,使用動態代理:
package controller;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;
import org.springframework.web.filter.OncePerRequestFilter;
public class EncodingFilter extends OncePerRequestFilter {
private String encoding;
public void setEncoding(String encoding) {
this.encoding = encoding;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
// 設定請求響應字元編碼
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
// 傳遞給目標servlet或jsp的實際上是動態代理的物件,而不是原始的HttpServletRequest物件
if("POST".equals(request.getMethod())){
//如果是post提交直接放行。
chain.doFilter(request, response);
}else{
//get提交進行處理
request = (HttpServletRequest) Proxy.newProxyInstance(request.getClass().getClassLoader(), request.getClass().getInterfaces(), new MyInvacationHandler(request));
chain.doFilter(request, response);
}
}
class MyInvacationHandler implements InvocationHandler{
private HttpServletRequest request;
MyInvacationHandler(HttpServletRequest request){
this.request=request;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
/*if("POST".equals(request.getMethod())){
return value;
}*/
switch (method.getName()) {
case "getParameter":
String value = request.getParameter((String)args[0]);
try {
if(value != null){
value=new String(value.getBytes("ISO-8859-1"),encoding);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
case "getParameterValues":
String[] values = request.getParameterValues((String)args[0]);
if (values != null) {
for (int i = 0; i < values.length; i++) {
try {
values[i] = new String(values[i].getBytes("ISO-8859-1"),encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
return values;
default:
return method.invoke(request, args);
}
}
}
}
web.xml配置檔案中
<filter-name>encodingFilter</filter-name>
<filter-class>controller.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
至此springmvc中的提交亂碼全部都解決。
在spring中,filter都預設繼承OncePerRequestFilter