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