Spring中的字符编码过滤器


Spring中的字符编码过滤器(CharacterEncodingFilter),可以很方便的为我们解决项目中出现的中文乱码问题;
使用方法也很简单,只需在web.xml文件中配置一下该过滤器,设置两个参数(encoding和forceEncoding)即可。
  1. <!-- 配置字符编码过滤器,编码格式设为UTF-8,避免中文乱码 -->  
  2. <filter>  
  3.     <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>  
  16.     <url-pattern>/*</url-pattern>  
  17. </filter-mapping>  
	<!-- 配置字符编码过滤器,编码格式设为UTF-8,避免中文乱码 -->
	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</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>
以下是Spring字符编码过滤器的源码:
  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.  *  Servlet 2.3/2.4 Filter允许为requests指定字符编码。 
  30.  *  这很有用,因为目前的浏览器通常不设置字符编码,虽然在HTML页面或表单中指定了。 
  31.  * <p>This filter can either apply its encoding if the request does not 
  32.  * already specify an encoding, or enforce this filter's encoding in any case 
  33.  * ("forceEncoding"="true"). In the latter case, the encoding will also be 
  34.  * applied as default response encoding on Servlet 2.4+ containers (although 
  35.  * this will usually be overridden by a full content type set in the view). 
  36.  *  这个过滤器,既可以在request没指定编码的情况下,应用其编码; 
  37.  *  也可以在任何情况下,强制应用其编码(“forceEncoding”= " true ")。 
  38.  *  在后一种情况下,在Servlet2.4以上的容器中,编码也会作为response的默认编码。 
  39.  * @author Juergen Hoeller 
  40.  * @since 15.03.2004 
  41.  * @see #setEncoding 
  42.  * @see #setForceEncoding 
  43.  * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding 
  44.  * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding 
  45.  */  
  46. public class CharacterEncodingFilter extends OncePerRequestFilter {  
  47.     private String encoding;  
  48.     private boolean forceEncoding = false;  
  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.          *  设置编码为request使用。这个编码将被传递给 
  54.          *  javax.servlet.http.HttpServletRequest#setCharacterEncoding调用。 
  55.      * <p>Whether this encoding will override existing request encodings 
  56.      * (and whether it will be applied as default response encoding as well) 
  57.      * depends on the {@link #setForceEncoding "forceEncoding"} flag. 
  58.          *  这个编码是否覆盖现有请求编码(以及是否会被应用为默认响应编码)取决于“forceEncoding”的设置。 
  59.      */  
  60.     public void setEncoding(String encoding) {  
  61.         this.encoding = encoding;  
  62.     }  
  63.   
  64.     /** 
  65.      * Set whether the configured {@link #setEncoding encoding} of this filter 
  66.      * is supposed to override existing request and response encodings. 
  67.          * 设置该过滤器的encoding配置是否应该覆盖现有的请求和响应编码。 
  68.      * <p>Default is "false", i.e. do not modify the encoding if 
  69.      * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()} 
  70.      * returns a non-null value. Switch this to "true" to enforce the specified 
  71.      * encoding in any case, applying it as default response encoding as well. 
  72.          *  默认是“false”,即当javax.servlet.http.HttpServletRequest#getCharacterEncoding() 
  73.          *  返回非空值时,不修改编码。将它切换到“true”,在任何情况下,都强制使用指定的编码, 
  74.          *  也将它作为默认响应编码。 
  75.      * <p>Note that the response encoding will only be set on Servlet 2.4+ 
  76.      * containers, since Servlet 2.3 did not provide a facility for setting 
  77.      * a default response encoding. 
  78.          *  注意,响应编码只能在Servlet 2.4以上才会被设置, 
  79.          *  因为Servlet 2.3没有提供一个工具来设置一个默认的响应编码。 
  80.      */  
  81.     public void setForceEncoding(boolean forceEncoding) {  
  82.         this.forceEncoding = forceEncoding;  
  83.     }  
  84.   
  85.     @Override  
  86.     protected void doFilterInternal(  
  87.             HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)  
  88.             throws ServletException, IOException {  
  89.   
  90.         if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {  
  91.             request.setCharacterEncoding(this.encoding);  
  92.             if (this.forceEncoding) {  
  93.                 response.setCharacterEncoding(this.encoding);  
  94.             }  
  95.         }  
  96.         filterChain.doFilter(request, response);  
  97.     }  
  98. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值