码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC配置字符过滤器的两种方式

时间:2017-12-01 17:40:54      阅读:518      评论:0      收藏:0      [点我收藏+]

标签:isp   ann   bsp   get   port   表单   提交表单   nbsp   imp   

有时候使用SpringMVC框架提交表单时会出现中文乱码,以下是我亲自试验过的配置字符过滤器的两种:

1、在web.xml中配置

<filter>  
<filter-name>characterEncodingFilter</filter-name>    
<filter-class>orgspringframeworkwebfilterCharacterEncodingFilter</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>characterEncodingFilter</filter-name>  
<url-pattern>/*</url-pattern>  
</filter-mapping>

2、JavaConfig

import javax.servlet.Filter;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class JavaSpringIn extends AbstractAnnotationConfigDispatcherServletInitializer {

    
     //配置字符过滤器
   @Override  
   protected Filter[] getServletFilters() {  
       CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();  
       characterEncodingFilter.setEncoding("UTF-8");  
       characterEncodingFilter.setForceEncoding(true);  
       return new Filter[] {characterEncodingFilter};  
   }     

}

 

SpringMVC配置字符过滤器的两种方式

标签:isp   ann   bsp   get   port   表单   提交表单   nbsp   imp   

原文地址:http://www.cnblogs.com/lwx521/p/7943565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!