Spring字符集过滤器CharacterEncodingFilter

  Spring中的字符集过滤器可以很方便的为我们解决项目中出现的中文乱码问题,而且使用方法也很简单,只需要在web.xml文件中配置一下该过滤器,设置两个重要的参数(encodingforceEncoding)即可:

[html] view plain copy
 
  1. <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->  
  2.     <filter>  
  3.        <filter-name>springUtf8Encoding</filter-name>  
  4.        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  5.        <init-param>  
  6.            <param-name>encoding</param-name>  
  7.            <param-value>UTF-8</param-value>  
  8.        </init-param>  
  9.        <init-param>  
  10.            <param-name>forceEncoding</param-name>  
  11.            <param-value>true</param-value>  
  12.        </init-param>   
  13.     </filter>  
  14.     <filter-mapping>  
  15.        <filter-name>springUtf8Encoding</filter-name>  
  16.        <url-pattern>/*</url-pattern>  
  17.    </filter-mapping>  

以下是Spring字符集过滤器的源码:

[java] view plain copy
 
  1. public class CharacterEncodingFilterextends OncePerRequestFilter {  
  2.    
  3.     private String encoding;  
  4.    
  5.     private boolean forceEncoding = false;  
  6.    
  7.    
  8.     /** 
  9.      * Set the encoding to usefor requests. This encoding will be passed into a 
  10.      * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call. 
  11.      * <p>Whether this encoding will overrideexisting request encodings 
  12.      * (and whether it will beapplied as default response encoding as well) 
  13.      * depends on the {@link #setForceEncoding "forceEncoding"} flag. 
  14.      */  
  15.     public void setEncoding(String encoding) {  
  16.        this.encoding = encoding;  
  17.     }  
  18.    
  19.     /** 
  20.      * Set whether theconfigured {@link #setEncoding encoding} of this filter 
  21.      * is supposed to overrideexisting request and response encodings. 
  22.      * <p>Default is "false", i.e. do notmodify the encoding if 
  23.      * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()} 
  24.      * returns a non-null value.Switch this to "true" to enforce the specified 
  25.      * encoding in any case,applying it as default response encoding as well. 
  26.      * <p>Note that the response encoding will onlybe set on Servlet 2.4+ 
  27.      * containers, sinceServlet 2.3 did not provide a facility for setting 
  28.      * a default responseencoding. 
  29.      */  
  30.     public void setForceEncoding(boolean forceEncoding) {  
  31.        this.forceEncoding = forceEncoding;  
  32.     }  
  33.    
  34.    
  35.     @Override  
  36.     protected void doFilterInternal(  
  37.            HttpServletRequest request, HttpServletResponse response,FilterChain filterChain)  
  38.            throws ServletException, IOException {  
  39.    
  40.        if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {  
  41.            request.setCharacterEncoding(this.encoding);  
  42.            if (this.forceEncoding) {  
  43.               response.setCharacterEncoding(this.encoding);  
  44.            }  
  45.        }  
  46.        filterChain.doFilter(request, response);  
  47.     }  
  48. }  

       由源码可以知道,该字符集过滤器有两个重要参数,分别是encodingforceEncoding,这两个参数分别有什么作用呢?

       以下是参考文档的介绍:

setEncoding

public voidsetEncoding(java.lang.String encoding)

Set the encodingto use for requests. This encoding will be passed into aServletRequest.setCharacterEncoding(java.lang.String) call.

setForceEncoding

public voidsetForceEncoding(boolean forceEncoding)

Set whether theconfigured encoding of this filter is supposed to override existing request andresponse encodings.

      通过参考文档,我们可以知道:

l  第一个方法setEncoding()相当于:ServletRequest.setCharacterEncoding(java.lang.String)

2. 第二个方法setForceEncoding()的作用是:

强制ServletResponse的编码格式和ServletRequest的编码格式一样。

        也就是说,无论是request还是responseencoding设置了两者的编码格式,只不过forceEncoding默认值为false,此时就只是设置了request的编码格式,即在Servlet中:

        request.setCharacterEncoding("XXXX"); 

       如果设置forceEncoding的值为true时,相当于Servlet中:

       request.setCharacterEncoding("XXXX");

       response.setCharacterEncoding(“XXXX”);  

      现在我们回过头来看看最初给大家看的web.xml中那部分过滤器的配置,相信大家都明白了,配置的作用相当于Servlet中的:

[java] view plain copy
 
  1. @RequestMapping(value="XXXXX")  
  2. public void XXXXX(User user,HttpServletRequestreq,HttpServletResponse resp) throws UnsupportedEncodingException  
  3. {  
  4.        resp.setCharacterEncoding("UTF-8");  
  5.        req.setCharacterEncoding("UTF-8");  
  6. ......  
  7. }  

       因此,在请求处理的过程中我们可以不用考虑编码方面的问题,上面两句代码可以省略,编码统一交给Spring过滤器去处理,我们可以专心处理我们的业务逻辑代码,这就是Spring字符集过滤器的方便之处。

原文地址:https://www.cnblogs.com/tian830937/p/5711938.html