一个简单的学习springmvc的demo中,当http请求传入中文参数时,在controller中接受到的参数就已经是乱码了,经百度一番解决方案如下:
1. get请求方式乱码解决
对于get方式,web.xml的filter配置是不起作用的,需要修改tomcat中的server.xml,将原来的
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
添加 URIEncoding="utf-8",修改为:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>
2. post方式(我自己验证通过上面方式也能解决post的问题)
<filter> <description>字符集过滤器</description> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>