SpringMVC_中文乱码的配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)
spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter用于解决POST方式造成的中文乱码
<filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
我的配置:
这个地方有一个问题,一般的小网站都有登陆过滤器,但是配置登陆过滤器的时候,会出现乱码,解决方案是
调整登陆过滤器和CharacterEncodingFilter的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>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.htm</url-pattern> </filter-mapping> //先配置org.springframework.web.filter.CharacterEncodingFilter再配置自定义的filter <filter> <filter-name>loginFilter</filter-name> <filter-class>tss.kz.filters.LoginFilter</filter-class> <init-param> <param-name>indexPath</param-name> <param-value>index.htm</param-value> </init-param> <init-param> <param-name>ignoreList</param-name> <param-value></param-value> </init-param> </filter> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>*.htm</url-pattern> </filter-mapping>