問題描述:
當傳遞中文參數到controller類時,無亂是get方式還是post方式都出現亂碼
解決:
1、保證所有的頁面編碼都是utf-8,包括jsp頁面,瀏覽器編碼設置和eclipse的編碼設置。
2、spingmvc給我們提供了一個編碼過濾器,只需要在配置文件web.xml中加入即可。如下:
<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>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、以上兩步有時只能解決post方式傳遞參數亂碼問題,get方式還是出現亂碼,則就需要該tomcat的配置文件了,打開tomcat的server.xml文件,找到以下行
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
在上面行中插入URIEncoding="UTF-8",改成如下形式:
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>