解決spring3 mvc中亂碼的問題
阿新 • • 發佈:2018-12-31
原文 http://blog.csdn.net/zhengzhb/article/details/6967975
今天搭建spring3 mvc框架的時候,發現居然又出現了亂碼(萬惡的iso編碼啊),想起之前解決post提交亂碼問題一般都是寫一個filter,在filter中實現編碼的轉換,配置方法是是在web.xml中加入以下程式碼:
- <filter>
- <filter-name>Character Encoding</filter-name>
-
<filter-class>com.filter.CharacterEncodingFilter
- <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>
其中:com.filter.CharacterEncodingFilter是自己實現的過濾器,具體寫法可以參考tomcat6中webapps\examples\WEB-INF\classes\filters下SetCharacterEncodingFilter這個類。
覺得這樣做有點麻煩,百度了一下,發spring中自帶了一個設定編碼的過濾器org.springframework.web.filter.CharacterEncodingFilter,所以在web.xml中可以改成這樣:
- <filter>
-
<
- <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>
這樣就不用自己寫過濾器了。
但是還是覺得有點麻煩,因為使用spring mvc的時候,已經在web.xml中設定了servlet:
- <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>
上面已經對請求做了一次過濾,如果再加一個過濾器感覺有點多餘,直接改一下DispatcherServlet這個類,使它具有修改編碼的功能不是更好?於是就寫了一個新的類,繼承自DispatcherServlet,程式碼如下:
- 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;
- publicclass EncodingDispatcherServlet extends DispatcherServlet {
- private String encoding;
- publicvoid init(ServletConfig config) throws ServletException {
- encoding = config.getInitParameter("encoding");
- super.init(config);
- }
- protectedvoid doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
- request.setCharacterEncoding(encoding);
- super.doService(request, response);
- }
- }
然後將web.xml中的配置改為:
- <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>
OK,亂碼消失,現在總結一下亂碼的問題,get方法亂碼解決方法一般是將tomcat中conf目錄下server.xml檔案中加入
- <Connectorport="8080"maxHttpHeaderSize="8192"
- maxThreads="150"minSpareThreads="25"maxSpareThreads="75"
- enableLookups="false"redirectPort="8443"acceptCount="100"
- connectionTimeout="20000"disableUploadTimeout="true"URIEncoding="UTF-8"/>
而post方法解決亂碼是設定一個專門處理編碼轉換的filter或者servlet。