Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)

来源:互联网 发布:手机申请淘宝店铺 编辑:程序博客网 时间:2024/04/29 17:24

  Spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,CharacterEncodingFilter源代码如下:

[java] view plaincopy
  1. /* 
  2.  * Copyright 2002-2007 the original author or authors. 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package org.springframework.web.filter;  
  18.   
  19. import java.io.IOException;  
  20. import javax.servlet.FilterChain;  
  21. import javax.servlet.ServletException;  
  22. import javax.servlet.http.HttpServletRequest;  
  23. import javax.servlet.http.HttpServletResponse;  
  24.   
  25. /** 
  26.  * Servlet 2.3/2.4 Filter that allows one to specify a character encoding for 
  27.  * requests. This is useful because current browsers typically do not set a 
  28.  * character encoding even if specified in the HTML page or form. 
  29.  * 
  30.  * <p>This filter can either apply its encoding if the request does not 
  31.  * already specify an encoding, or enforce this filter's encoding in any case 
  32.  * ("forceEncoding"="true"). In the latter case, the encoding will also be 
  33.  * applied as default response encoding on Servlet 2.4+ containers (although 
  34.  * this will usually be overridden by a full content type set in the view). 
  35.  * 
  36.  * @author Juergen Hoeller 
  37.  * @since 15.03.2004 
  38.  * @see #setEncoding 
  39.  * @see #setForceEncoding 
  40.  * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding 
  41.  * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding 
  42.  */  
  43. public class CharacterEncodingFilter extends OncePerRequestFilter {  
  44.   
  45.     private String encoding;  
  46.   
  47.     private boolean forceEncoding = false;  
  48.   
  49.   
  50.     /** 
  51.      * Set the encoding to use for requests. This encoding will be passed into a 
  52.      * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call. 
  53.      * <p>Whether this encoding will override existing request encodings 
  54.      * (and whether it will be applied as default response encoding as well) 
  55.      * depends on the {@link #setForceEncoding "forceEncoding"} flag. 
  56.      */  
  57.     public void setEncoding(String encoding) {  
  58.         this.encoding = encoding;  
  59.     }  
  60.   
  61.     /** 
  62.      * Set whether the configured {@link #setEncoding encoding} of this filter 
  63.      * is supposed to override existing request and response encodings. 
  64.      * <p>Default is "false", i.e. do not modify the encoding if 
  65.      * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()} 
  66.      * returns a non-null value. Switch this to "true" to enforce the specified 
  67.      * encoding in any case, applying it as default response encoding as well. 
  68.      * <p>Note that the response encoding will only be set on Servlet 2.4+ 
  69.      * containers, since Servlet 2.3 did not provide a facility for setting 
  70.      * a default response encoding. 
  71.      */  
  72.     public void setForceEncoding(boolean forceEncoding) {  
  73.         this.forceEncoding = forceEncoding;  
  74.     }  
  75.   
  76.   
  77.     @Override  
  78.     protected void doFilterInternal(  
  79.             HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)  
  80.             throws ServletException, IOException {  
  81.   
  82.         if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {  
  83.             request.setCharacterEncoding(this.encoding);  
  84.             if (this.forceEncoding) {  
  85.                 response.setCharacterEncoding(this.encoding);  
  86.             }  
  87.         }  
  88.         filterChain.doFilter(request, response);  
  89.     }  
  90.   
  91. }  

        上述代码显示,在配置字符集过滤器时可设定两个参数的值,如下:

        l  encoding:字符集,即将过滤到的request的字符集设置为encoding指定的值,如UTF-8等,相当于:

[java] view plaincopy
  1. request.setCharacterEncoding  

        l  forceEncoding:字面意思是强制字符集,但你大可不必按字面意思理解,因为这个参数的值只不过是指定response的字符集是否也设置成encoding所指定的字符集,所以你可以选择设置为true或false,当值为true时,相当于

[java] view plaincopy
  1. request.setCharacterEncoding(“”);  
  2. response.setCharacterEncoding(“”);  

        当值为false时,相当于:

[java] view plaincopy
  1. request.setCharacterEncoding(“”);  

        默认值为false。

        示例:

[java] view plaincopy
  1.        <filter>  
  2.     <filter-name>characterEncodingFilter</filter-name>  
  3.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  4.     <init-param>  
  5.         <param-name>encoding</param-name>  
  6.         <param-value>UTF-8</param-value>  
  7.     </init-param>  
  8.     <init-param>  
  9.         <param-name>forceEncoding</param-name>  
  10.         <param-value>true</param-value>  
  11.     </init-param>  
  12. </filter>  
  13. <filter-mapping>  
  14.     <filter-name>characterEncodingFilter</filter-name>  
  15.     <url-pattern>/*</url-pattern>  
  16. </filter-mapping>  

        以上代码放置在web.xml中,相当于servlet中的:

[java] view plaincopy
  1. request.setCharacterEncoding("UTF-8");  
  2. response.setCharacterEncoding("UTF-8");  
0 0
原创粉丝点击