由于在Spring MVC开发中大量用GB2312出现此问题,解决方法可以有下方法。
统一使用UTF-8
1.在web.xml中过滤编码格式,代码如下
Xml代码
- <!--编码过滤器-->
- <filter>
- <filter-name>encodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>false</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>*.html</url-pattern>
- </filter-mapping>
- <!--编码过滤器-->
- <filter>
- <filter-name>encodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>false</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>*.html</url-pattern>
- </filter-mapping>
2.jsp页面的编码格式全部用UTF-8
3.控制器直接用request.getparamer()接收
【编辑推荐】