1. 程式人生 > >關於springMVC中GET請求時出現中文亂碼的問題

關於springMVC中GET請求時出現中文亂碼的問題

專案中的web.xml中的編碼設定為:

 <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

但這個設定是針對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"/>

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

參考 https://www.cnblogs.com/liukemng/p/4178882.html