zoukankan      html  css  js  c++  java
  • Spring字符集过滤器CharacterEncodingFilter(转)

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

    Xml代码  
    1. <!-- 配置请求过滤器 ,编码格式设为UTF-8,避免中文乱码-->  
    2.   
    3.     <filter>  
    4.   
    5.        <filter-name>springUtf8Encoding</filter-name>  
    6.   
    7.        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    8.   
    9.        <init-param>  
    10.   
    11.            <param-name>encoding</param-name>  
    12.   
    13.            <param-value>UTF-8</param-value>  
    14.   
    15.        </init-param>  
    16.   
    17.        <init-param>  
    18.   
    19.            <param-name>forceEncoding</param-name>  
    20.   
    21.            <param-value>true</param-value>  
    22.   
    23.        </init-param>    
    24.   
    25.     </filter>  
    26.   
    27.     <filter-mapping>  
    28.   
    29.        <filter-name>springUtf8Encoding</filter-name>  
    30.   
    31.        <url-pattern>/*</url-pattern>  
    32.   
    33.  </filter-mapping>  

     以下是

    Spring字符集过滤器的源码:

    Java代码  
    1. public class CharacterEncodingFilter extends OncePerRequestFilter {  
    2.   
    3.    
    4.   
    5.     private String encoding;  
    6.   
    7.    
    8.   
    9.     private boolean forceEncoding = false;  
    10.   
    11.    
    12.   
    13.    
    14.   
    15.     /** 
    16.  
    17.      * Set the encoding to use for requests. This encoding will be passed into a 
    18.  
    19.      * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call. 
    20.  
    21.      * <p>Whether this encoding will override existing request encodings 
    22.  
    23.      * (and whether it will be applied as default response encoding as well) 
    24.  
    25.      * depends on the {@link #setForceEncoding "forceEncoding"} flag. 
    26.  
    27.      */  
    28.   
    29.     public void setEncoding(String encoding) {  
    30.   
    31.        this.encoding = encoding;  
    32.   
    33.     }  
    34.   
    35.    
    36.   
    37.     /** 
    38.  
    39.      * Set whether the configured {@link #setEncoding encoding} of this filter 
    40.  
    41.      * is supposed to override existing request and response encodings. 
    42.  
    43.      * <p>Default is "false", i.e. do not modify the encoding if 
    44.  
    45.      * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()} 
    46.  
    47.      * returns a non-null value. Switch this to "true" to enforce the specified 
    48.  
    49.      * encoding in any case, applying it as default response encoding as well. 
    50.  
    51.      * <p>Note that the response encoding will only be set on Servlet 2.4+ 
    52.  
    53.      * containers, since Servlet 2.3 did not provide a facility for setting 
    54.  
    55.      * a default response encoding. 
    56.  
    57.      */  
    58.   
    59.     public void setForceEncoding(boolean forceEncoding) {  
    60.   
    61.        this.forceEncoding = forceEncoding;  
    62.   
    63.     }  
    64.   
    65.    
    66.   
    67.    
    68.   
    69.     @Override  
    70.   
    71.     protected void doFilterInternal(  
    72.   
    73.            HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)  
    74.   
    75.            throws ServletException, IOException {  
    76.   
    77.    
    78.   
    79.        if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {  
    80.   
    81.            request.setCharacterEncoding(this.encoding);  
    82.   
    83.            if (this.forceEncoding) {  
    84.   
    85.               response.setCharacterEncoding(this.encoding);  
    86.   
    87.            }  
    88.   
    89.        }  
    90.   
    91.        filterChain.doFilter(request, response);  
    92.   
    93.     }  
    94.   
    95. }  

         由源码可以知道,该字符集过滤器有两个重要参数,分别是

    encodingforceEncoding,这两个参数分别有什么作用呢?

    以下是参考文档的介绍: 

    lsetEncoding

    public void setEncoding(java.lang.String encoding)

    Set the encoding to use for requests. This encoding will be passed into a ServletRequest.setCharacterEncoding(java.lang.String) call.

    lsetForceEncoding

    public void setForceEncoding(boolean forceEncoding)

    Set whether the configured encoding of this filter is supposed to override existing request and response encodings.

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

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

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

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

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

    Java代码  
    1. request.setCharacterEncoding("XXXX");    
    Java代码  
    1.    

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

    Java代码  
    1. request.setCharacterEncoding("XXXX");  
    2.   
    3. response.setCharacterEncoding(“XXXX”);    

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

    Java代码  
    1. @RequestMapping(value="XXXXX")  
    2. public void XXXXMethod(User user,HttpServletRequest req,HttpServletResponse resp) throws UnsupportedEncodingException  
    3. {  
    4.         resp.setCharacterEncoding("UTF-8");  
    5.         req.setCharacterEncoding("UTF-8");  
    6. ......  
    7. }  

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

  • 相关阅读:
    Flink 状态生命周期
    jpa使用@CollectionTable创建表
    Java的四种引用---强软弱虚
    ThreadLocal与内存泄露
    Flink 1.11 Table API 实现kafka到mysql
    FLIink 1.11 SQL 构建一个端到端的流式应用
    Flink1.11编译
    Flink运行yarn-session报错 java.lang.NoClassDefFoundError: org/apache/hadoop/yarn/exceptions/YarnException
    欢迎订阅AI科技导读微信公众号,获取人工智能的最新技术解读教程!
    深度学习深刻理解和应用--人工智能从业人员必看知识
  • 原文地址:https://www.cnblogs.com/cornucopia/p/4502679.html
Copyright © 2011-2022 走看看