SpringMVC学习 && 乱码问题解决
程序员文章站
2024-02-09 16:11:28
...
测试代码:hello页面只负责取msg数据
<form action="/e1" method="post">
<input type="text" name="name">
<input type="submit">
</form>
@Controller
public class EncodingController {
@RequestMapping("/e1")
public String test(String name, Model model){
model.addAttribute("msg",name);
return "hello";
}
}
测试数据:
测试结果:
解决方式一:自己写编码过滤器
public class EncodingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException { }
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() { }
}
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.Nana.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
缺点:不够强大
解决方式二:使用SpringMVC编码过滤器
<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支持不好
解决方式三:使用别人写好的过滤器
-
修改tomcat配置文件,设置编码
<Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
-
自定义过滤器
package com.Nana.filter; 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.util.Map; /** * 解决get和post请求 全部乱码的过滤器 */ public class GenericEncodingFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //处理response的字符编码 HttpServletResponse myResponse=(HttpServletResponse) response; myResponse.setContentType("text/html;charset=UTF-8"); // 转型为与协议相关对象 HttpServletRequest httpServletRequest = (HttpServletRequest) request; // 对request包装增强 HttpServletRequest myrequest = new MyRequest(httpServletRequest); chain.doFilter(myrequest, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } } //自定义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(); if (method.equalsIgnoreCase("post")) { // post请求 try { // 处理post乱码 request.setCharacterEncoding("utf-8"); return request.getParameterMap(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else if (method.equalsIgnoreCase("get")) { // get请求 Map<String, String[]> parameterMap = request.getParameterMap(); if (!hasEncode) { // 确保get手动编码逻辑只运行一次 for (String parameterName : parameterMap.keySet()) { String[] values = parameterMap.get(parameterName); if (values != null) { for (int i = 0; i < values.length; i++) { try { // 处理get乱码 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中配置这个过滤器
/和/*区别:
-
/会匹配到/springmvc这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
-
会匹配所有的url:路径型的和后缀型的url(包括/springmvc,.jsp,.js和*.html等)
-
两个符号要慎重选用
上一篇: 访问github 慢问题解决
下一篇: vue过滤器filter
推荐阅读
-
问题解决学习笔记
-
SpringMVC学习 && 乱码问题解决
-
php miniBB中文乱码问题解决方法_PHP
-
mysql字符集乱码问题解决方法介绍_MySQL
-
mysql中文乱码问题解决方法
-
Java URL抓取页面内容乱码问题解决办法 博客分类: java JavaOracleITeyeYahooHTML
-
php写入mysql中文乱码问题解决办法
-
SpringMVC4学习目录 博客分类: SpringMVC springRESTIOC
-
jdcb查询SQL Server数据乱码问题解决方法 博客分类: Oracle&MSSQL sql server
-
request.getInputStream()乱码问题解决方案(未解决)