创建测试页面
- 创建一个form 表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/springmvc_04/encoding/t1" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
注意:想要直接访问.jsp 页面,页面必须创建者 web 根目录下!
- 创建参数接收方法
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class EncodingController {
@PostMapping("/encoding/t1")
public String test1(String name, Model model){
model.addAttribute("msg", name);
return "test";
}
}
- 创建测试jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
-
启动程序,form表单直接输入中文,提交表单,可以看到结果乱码!
-
问题排查
打开浏览器调试,发现返回结果本身就是乱码
java 后台打印参数
发现java后台接收到的参数就是乱码!
解决方案
- 确定浏览器编码为UTF-8
- 检查Tomcat 配置,tomcat 安装目录...apache-tomcat-7.0.82\conf 下找到 server.xml检查编码配置
如果没有 URIEncoding="UTF-8" 则需要手动添加
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/>
- 配置SpringMVC为我们提供的编码过滤器,直接在web.xml 中配置即可!
<url-pattern>/</url-pattern> 必须为 /* 否则jsp页面无效!
<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>
- 如果还不能解决,则更换过滤器!
去掉SpringMVC过滤器
添加自定义过滤器类
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse myResponse = (HttpServletResponse) servletResponse;
myResponse.setContentType("text/html;charset=UTF-8");
// 转换类型为协议相关对象
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
// 对request包装增强
HttpServletRequest myRequest = new MyRequest(httpServletRequest);
filterChain.doFilter(myRequest, servletResponse);
}
@Override
public void destroy() {
}
}
// 自定义request对象,HttpServletRequest 的包装类
class MyRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
// 是否编码的标记
private boolean hasEncode;
// 定义一个可以传入HttpServletRequest 对象的构造函数,一边对其进行装饰
public MyRequest(HttpServletRequest request){
super(request); // super 必须写
this.request = request;
}
// 对需要增强的方法进行覆盖
@Override
public Map getParameterMap(){
// 获取请求方式
String method = request.getMethod();
// post 请求
if(method.equalsIgnoreCase("post")){
//处理post乱码
try{
request.setCharacterEncoding("utf-8");
return request.getParameterMap();
} catch (UnsupportedEncodingException e){
e.printStackTrace();
}
// get 请求
} else if(method.equalsIgnoreCase("get")){
Map<String, String[]> parameterMap = request.getParameterMap();
if(!hasEncode){
for (String parameterName : parameterMap.keySet()){
String[] values = parameterMap.get(parameterName);
if (values != null){
for (int i = 0; i < values.length; i ++){
// 处理get 乱码
try {
values[i] = new String(values[i].getBytes("ISO-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
}
}
hasEncode = true;
}
return parameterMap;
}
return super.getParameterMap();
}
// 取一个值
@Override
public String getParameter(String name){
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
if(values == null){
return null;
}
return values[0]; // 取回参数的第一个值
}
@Override
public String[] getParameterValues(String name){
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
return values;
}
}
web.xml 配置自定义过滤器
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.hunter.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
网友评论