今天搭建spring3 mvc框架的时候,发现居然又出现了乱码(万恶的iso编码啊),想起之前解决post提交乱码问题一般都是写一个filter,在filter中实现编码的转换,配置方法是是在web.xml中加入以下代码:
<span style="font-size:18px;"> <filter> <filter-name>Character Encoding</filter-name> <filter-class>com.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </span>
其中:com.filter.CharacterEncodingFilter是自己实现的过滤器,具体写法可以参考tomcat6中webapps\examples\WEB-INF\classes\filters下SetCharacterEncodingFilter这个类。
觉得这样做有点麻烦,百度了一下,发spring中自带了一个设置编码的过滤器org.springframework.web.filter.CharacterEncodingFilter,所以在web.xml中可以改成这样:
<span style="font-size:18px;"> <filter> <filter-name>Character 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>Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </span>
这样就不用自己写过滤器了。
但是还是觉得有点麻烦,因为使用spring mvc的时候,已经在web.xml中设置了servlet:
<span style="font-size:18px;"> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </span>
上面已经对请求做了一次过滤,如果再加一个过滤器感觉有点多余,直接改一下DispatcherServlet这个类,使它具有修改编码的功能不是更好?于是就写了一个新的类,继承自DispatcherServlet,代码如下:
<span style="font-size:18px;">package com.mocha.servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.DispatcherServlet; public class EncodingDispatcherServlet extends DispatcherServlet { private String encoding; public void init(ServletConfig config) throws ServletException { encoding = config.getInitParameter("encoding"); super.init(config); } protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding(encoding); super.doService(request, response); } } </span>
然后将web.xml中的配置改为:
<span style="font-size:18px;"> <servlet> <servlet-name>spring</servlet-name> <servlet-class>com.mocha.servlet.EncodingDispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </span>
OK,乱码消失,现在总结一下乱码的问题,get方法乱码解决方法一般是将tomcat中conf目录下server.xml文件中加入
<span style="font-size:18px;"><Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" /> </span>
而post方法解决乱码是设置一个专门处理编码转换的filter或者servlet。