1.Spring 默认的字符编码格式为iso-8859-1,为此Spring专门提供了字符过滤器org.springframework.web.filter.CharacterEncodingFilter,且只对post请求生效
a.在项目的web.xml中配置Spring提供的过滤器
1 <filter> 2 <filter-name>characterEncodingFilter</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 <init-param> 9 <param-name>forceEncoding</param-name> 10 <param-value>true</param-value> 11 </init-param> 12 </filter> 13 <filter-mapping> 14 <filter-name>characterEncodingFilter</filter-name> 15 <url-pattern>/*<url-pattern>
</filter-mapping>
2.检查自己tomcat中的conf/server.xml
tomcat :
1 把 2 <Connector port="8080" protocol="HTTP/1.1" 3 connectionTimeout="20000" 4 redirectPort="8443" /> 5 改成 6 <Connector port="8080" protocol="HTTP/1.1" 7 connectionTimeout="20000" 8 redirectPort="8443" URIEncoding="UTF-8"/>
如果是在maven中用tomcat,在pom.xml配置的插件如下
1 <plugin> 2 <groupId>org.apache.tomcat.maven</groupId> 3 <artifactId>tomcat7-maven-plugin</artifactId> 4 <version>2.2</version> 5 <configuration> 6 <uriEncoding>UTF-8</uriEncoding> 7 </configuration> 8 </plugin>
3.如果还发发生乱码,我们就要回到1中好好理解Spring专门提供的字符过滤器有什么限制,使得该过滤器不生效
1 Spring专门提供的字符过滤器只对post请求生效