我的博客:www.while0.com
今天测试spring mvc ,中文乱码。根据网上的建议,更改过滤器,在web.xml中加上
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> 16 </filter-mapping>
过滤所有请求,但还是有问题,最后发现是表单的问题,在表单中method默认是get,而以上过滤器只对post有效果,如果是get,需要修改 Tomcat根式目录的 conf/server.xml文件中,找<Connector port="8080" />,在里面加uRIEncoding="utf-8"
方法二,controller代码里加入contentValue = new String(new String(contentValue.getBytes("ISO-8859-1"), "UTF-8"); contentValue为参数