Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)
spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,CharacterEncodingFilter源代码如下:
- /*
- * Copyright 2002-2007 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.springframework.web.filter;
- import java.io.IOException;
- import javax.servlet.FilterChain;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * 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.
- *
- * <p>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).
- *
- * @author Juergen Hoeller
- * @since 15.03.2004
- * @see #setEncoding
- * @see #setForceEncoding
- * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding
- * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding
- */
- public class CharacterEncodingFilter extends OncePerRequestFilter {
- private String encoding;
- private boolean forceEncoding = false;
- /**
- * Set the encoding to use for requests. This encoding will be passed into a
- * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
- * <p>Whether this encoding will override existing request encodings
- * (and whether it will be applied as default response encoding as well)
- * depends on the {@link #setForceEncoding "forceEncoding"} flag.
- */
- public void setEncoding(String encoding) {
- this.encoding = encoding;
- }
- /**
- * Set whether the configured {@link #setEncoding encoding} of this filter
- * is supposed to override existing request and response encodings.
- * <p>Default is "false", i.e. do not modify the encoding if
- * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
- * returns a non-null value. Switch this to "true" to enforce the specified
- * encoding in any case, applying it as default response encoding as well.
- * <p>Note that the response encoding will only be set on Servlet 2.4+
- * containers, since Servlet 2.3 did not provide a facility for setting
- * a default response encoding.
- */
- public void setForceEncoding(boolean forceEncoding) {
- this.forceEncoding = forceEncoding;
- }
- @Override
- protected void doFilterInternal(
- HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
- if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
- request.setCharacterEncoding(this.encoding);
- if (this.forceEncoding) {
- response.setCharacterEncoding(this.encoding);
- }
- }
- filterChain.doFilter(request, response);
- }
- }
上述代码显示,在配置字符集过滤器时可设定两个参数的值,如下:
l encoding:字符集,即将过滤到的request的字符集设置为encoding指定的值,如UTF-8等,相当于:
- request.setCharacterEncoding
l
forceEncoding:字面意思是强制字符集,但你大可不必按字面意思理解,因为这个参数的值只不过是指定response的字符集是否也设置成
encoding所指定的字符集,所以你可以选择设置为true或false,当值为true时,相当于
- request.setCharacterEncoding(“”);
- response.setCharacterEncoding(“”);
当值为false时,相当于:
- request.setCharacterEncoding(“”);
默认值为false。
示例:
- <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>
以上代码放置在web.xml中,相当于servlet中的:
- request.setCharacterEncoding("UTF-8");
- response.setCharacterEncoding("UTF-8");
Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)的更多相关文章
- spring mvc设置字符集过滤器
<filter> <filter-name>springEncoding</filter-name> <filter-class> org.spring ...
- Spring字符集过滤器CharacterEncodingFilter
Spring中的字符集过滤器可以很方便的为我们解决项目中出现的中文乱码问题,而且使用方法也很简单,只需要在web.xml文件中配置一下该过滤器,设置两个重要的参数(encoding和forceEnco ...
- Spring MVC【入门】就这一篇!
MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...
- Spring MVC实践
MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...
- Spring MVC【入门】
Spring MVC[入门]就这一篇! MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Mod ...
- Spring MVC【入门】一篇!
MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP ...
- Spring04——Spring MVC 全解析
前文分别介绍了 Spring IOC 与 Spring AOP 的相关知识,本文将为各位大概带来 Spring MVC 的知识点.关注我的公众号「Java面典」,每天 10:24 和你一起了解更多 J ...
- Spring MVC配置DispatcherServlet的url-pattern
在配置Spring MVC的核心过滤器DispatcherServlet的url-pattern时是有要求的. <servlet> <servlet-name>...</ ...
- Spring MVC 函数式编程进阶
1. 前言 上一篇对 Spring MVC 的函数式接口编程进行了简单入门,让很多不知道的同学见识了这种新操作.也有反应这种看起来没有传统写法顺眼,其实大家都一样.但是我们还是要敢于尝试新事物.Jav ...
随机推荐
- JavaScript - UnderScore
UnderScore 第一步 call(this) (function() {}.call(this)); 一些简单的初始化操作 (function() { var root = this; var ...
- [SQL]oracle 的to_char、to_number、to_date用法
关键字: oracle 的to_char.to_number.to_date用法 TO_CHAR 是把日期或数字转换为字符串TO_DATE 是把字符串转换为数据库中得日期类型转换函数TO_NUMBER ...
- the last lecture
2008.07.25,CMU教授Randy Pausch教授因癌症去世,仅47岁. 几年之前,当我看到Pausch先生最后一课的视频时,让我震撼. 转眼之间,7年过去了,这7年,让我成长了许多. 7年 ...
- android倒计时(整理)
android倒计时 用到CountDownTimer Android中文API(143) —— CountDownTimer 前言 本章内容android.os.CountDownTime章节,版本 ...
- 第一个java程序hello world
首先需要配置环境,没配置的请参考:详细配置教程:http://www.cnblogs.com/qq1871707128/p/6047232.html 切入主题: java基础首先得了解基本的dos命令 ...
- X64下MmIsAddressValid的逆向及内存寻址解析
标 题: [原创]X64下MmIsAddressValid的逆向及内存寻址解析 作 者: 普通朋友 时 间: 2015-10-21,20:03:52 链 接: http://bbs.pediy.com ...
- tab_切换
记忆: 一.这里用到了jQuery遍历---filter()方法: filter() 方法将匹配元素集合缩减为匹配指定选择器的元素. 二.HTML DOM hash属性 hash 属性是一个可读可写的 ...
- zookeeper源码分析(一) 工作原理
来自:http://www.codedump.info/?p=207 阅读zookeeper代码一段时间(注:是很长一段时间,断断续续得有半年了吧?)之后,我要开始将一些积累下来的东西写下来了,鉴于我 ...
- 关于解决haswell赛扬和奔腾 不能安装的问题
打开EFI\CLOVER\config.plist,并找到KernelAndKextPatches字段,在子集内插入下面代码. <key>FakeCPUID</key> < ...
- Liferay 6.2 改造系列之十:修改系统登录相关配置
1.关闭自动登录功能: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Set this to true to allo ...