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

2020-09-20 20:25:20 浏览数 (1)

一、在web.xml中配置
代码语言:javascript复制
<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>
二、JavaConfig
代码语言:javascript复制
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};  
   }     

}

0 人点赞