CharacterEncodingFilter-Spring字符编码过滤器
用于处理项目中的乱码问题。
关系:
java.lang.Object
extended by org.springframework.web.filter.GenericFilterBean
extended by org.springframework.web.filter.OncePerRequestFilter
extended by org.springframework.web.filter.CharacterEncodingFilter
源码:
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 }
官方解释:
Servlet 2.3/2.4 Filter that allows one to specify a character encoding for requests. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form.
This filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case
("forceEncoding"="true"). In the latter case, the encoding will also be applied as default response encoding on Servlet 2.4+ containers (although this will usually be overridden by a full content type set in the view).
通过源码可以看到在web.xml配置CharacterEncodingFilter 时,可以配置两个参数:encoding和forceEncoding ;
encoding:编码格式;
forceEncoding :是否允许设置的encoding 覆盖request和response中已经存在的encodings。
如何使用:
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>