天天看點

SpringMVC遇到的亂碼問題- 解決GET請求時中文亂碼的問題 轉自http://www.cnblogs.com/liukemng/p/4178882.html

轉自http://www.cnblogs.com/liukemng/p/4178882.html

之前項目中的web.xml中的編碼設定:

SpringMVC遇到的亂碼問題- 解決GET請求時中文亂碼的問題 轉自http://www.cnblogs.com/liukemng/p/4178882.html
<filter>
        <filter-name>CharacterEncoding</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>      
SpringMVC遇到的亂碼問題- 解決GET請求時中文亂碼的問題 轉自http://www.cnblogs.com/liukemng/p/4178882.html

但這個設定是針對POST請求的,tomacat對GET和POST請求處理方式是不同的,要處理針對GET請求的編碼問題,則需要改tomcat的server.xml配置檔案,如下:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>      

改為:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>      

最關鍵的點在這裡:

如果你是更改的tomcat安裝目錄的server.xml配置檔案,那麼在用myeclipse時起作用,但在使用eclipse運作項目時會發現配置沒起作用,其實是因為eclipse在運作項目時是用的eclipse中配置的tomcat,那麼問題就好解決了,打開eclipse中的tomcat配置檔案,改為如下即可:

SpringMVC遇到的亂碼問題- 解決GET請求時中文亂碼的問題 轉自http://www.cnblogs.com/liukemng/p/4178882.html

注:配置useBodyEncodingForURI="true"後,可以解決普通get請求的中文亂碼問題,但是對于通過ajax發起的get請求中文依然會亂碼,請把useBodyEncodingForURI="true"改為URIEncoding="UTF-8"即可。

繼續閱讀