spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,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. package org.springframework.web.filter;
  17. import java.io.IOException;
  18. import javax.servlet.FilterChain;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. /**
  23. * Servlet 2.3/2.4 Filter that allows one to specify a character encoding for
  24. * requests. This is useful because current browsers typically do not set a
  25. * character encoding even if specified in the HTML page or form.
  26. *
  27. * <p>This filter can either apply its encoding if the request does not
  28. * already specify an encoding, or enforce this filter's encoding in any case
  29. * ("forceEncoding"="true"). In the latter case, the encoding will also be
  30. * applied as default response encoding on Servlet 2.4+ containers (although
  31. * this will usually be overridden by a full content type set in the view).
  32. *
  33. * @author Juergen Hoeller
  34. * @since 15.03.2004
  35. * @see #setEncoding
  36. * @see #setForceEncoding
  37. * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding
  38. * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding
  39. */
  40. public class CharacterEncodingFilter extends OncePerRequestFilter {
  41. private String encoding;
  42. private boolean forceEncoding = false;
  43. /**
  44. * Set the encoding to use for requests. This encoding will be passed into a
  45. * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
  46. * <p>Whether this encoding will override existing request encodings
  47. * (and whether it will be applied as default response encoding as well)
  48. * depends on the {@link #setForceEncoding "forceEncoding"} flag.
  49. */
  50. public void setEncoding(String encoding) {
  51. this.encoding = encoding;
  52. }
  53. /**
  54. * Set whether the configured {@link #setEncoding encoding} of this filter
  55. * is supposed to override existing request and response encodings.
  56. * <p>Default is "false", i.e. do not modify the encoding if
  57. * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
  58. * returns a non-null value. Switch this to "true" to enforce the specified
  59. * encoding in any case, applying it as default response encoding as well.
  60. * <p>Note that the response encoding will only be set on Servlet 2.4+
  61. * containers, since Servlet 2.3 did not provide a facility for setting
  62. * a default response encoding.
  63. */
  64. public void setForceEncoding(boolean forceEncoding) {
  65. this.forceEncoding = forceEncoding;
  66. }
  67. @Override
  68. protected void doFilterInternal(
  69. HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  70. throws ServletException, IOException {
  71. if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
  72. request.setCharacterEncoding(this.encoding);
  73. if (this.forceEncoding) {
  74. response.setCharacterEncoding(this.encoding);
  75. }
  76. }
  77. filterChain.doFilter(request, response);
  78. }
  79. }

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

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

  1. request.setCharacterEncoding


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

  1. request.setCharacterEncoding(“”);
  2. response.setCharacterEncoding(“”);

当值为false时,相当于:

  1. request.setCharacterEncoding(“”);

默认值为false。

示例:

  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中的:

    1. request.setCharacterEncoding("UTF-8");
    2. response.setCharacterEncoding("UTF-8");

Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)的更多相关文章

  1. spring mvc设置字符集过滤器

    <filter> <filter-name>springEncoding</filter-name> <filter-class> org.spring ...

  2. Spring字符集过滤器CharacterEncodingFilter

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

  3. Spring MVC【入门】就这一篇!

    MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...

  4. Spring MVC实践

    MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...

  5. Spring MVC【入门】

    Spring MVC[入门]就这一篇! MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Mod ...

  6. Spring MVC【入门】一篇!

    MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1:     出现的弊端: JSP ...

  7. Spring04——Spring MVC 全解析

    前文分别介绍了 Spring IOC 与 Spring AOP 的相关知识,本文将为各位大概带来 Spring MVC 的知识点.关注我的公众号「Java面典」,每天 10:24 和你一起了解更多 J ...

  8. Spring MVC配置DispatcherServlet的url-pattern

    在配置Spring MVC的核心过滤器DispatcherServlet的url-pattern时是有要求的. <servlet> <servlet-name>...</ ...

  9. Spring MVC 函数式编程进阶

    1. 前言 上一篇对 Spring MVC 的函数式接口编程进行了简单入门,让很多不知道的同学见识了这种新操作.也有反应这种看起来没有传统写法顺眼,其实大家都一样.但是我们还是要敢于尝试新事物.Jav ...

随机推荐

  1. JavaScript - UnderScore

    UnderScore 第一步 call(this) (function() {}.call(this)); 一些简单的初始化操作 (function() { var root = this; var ...

  2. [SQL]oracle 的to_char、to_number、to_date用法

    关键字: oracle 的to_char.to_number.to_date用法 TO_CHAR 是把日期或数字转换为字符串TO_DATE 是把字符串转换为数据库中得日期类型转换函数TO_NUMBER ...

  3. the last lecture

    2008.07.25,CMU教授Randy Pausch教授因癌症去世,仅47岁. 几年之前,当我看到Pausch先生最后一课的视频时,让我震撼. 转眼之间,7年过去了,这7年,让我成长了许多. 7年 ...

  4. android倒计时(整理)

    android倒计时 用到CountDownTimer Android中文API(143) —— CountDownTimer 前言 本章内容android.os.CountDownTime章节,版本 ...

  5. 第一个java程序hello world

    首先需要配置环境,没配置的请参考:详细配置教程:http://www.cnblogs.com/qq1871707128/p/6047232.html 切入主题: java基础首先得了解基本的dos命令 ...

  6. X64下MmIsAddressValid的逆向及内存寻址解析

    标 题: [原创]X64下MmIsAddressValid的逆向及内存寻址解析 作 者: 普通朋友 时 间: 2015-10-21,20:03:52 链 接: http://bbs.pediy.com ...

  7. tab_切换

    记忆: 一.这里用到了jQuery遍历---filter()方法: filter() 方法将匹配元素集合缩减为匹配指定选择器的元素. 二.HTML DOM hash属性 hash 属性是一个可读可写的 ...

  8. zookeeper源码分析(一) 工作原理

    来自:http://www.codedump.info/?p=207 阅读zookeeper代码一段时间(注:是很长一段时间,断断续续得有半年了吧?)之后,我要开始将一些积累下来的东西写下来了,鉴于我 ...

  9. 关于解决haswell赛扬和奔腾 不能安装的问题

    打开EFI\CLOVER\config.plist,并找到KernelAndKextPatches字段,在子集内插入下面代码. <key>FakeCPUID</key> < ...

  10. Liferay 6.2 改造系列之十:修改系统登录相关配置

    1.关闭自动登录功能: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Set this to true to allo ...