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. Win7下的内置FTP组件的设置详解

    在局域网中共享文件,FTP是比较方便的方案之一.Win7内部集成了FTP,只是设置起来颇费一番功夫.着文以记之. 一.安装FTP组件 由于Win7默认没有安装FTP组件.故FTP的设置第一步就是安装F ...

  2. Linux光纖卡配置,磁盤掛載,多路徑設置

    Linux光纖卡配置 1.首先根據光纖卡類型加載對應的驅動.我這裡常用的是QLogic和Brocade光纖卡 [root@rhcsasm2 host3]# lspci | grep Fibre   - ...

  3. Java 体系结构

    Java体系结构包括四个独立但相关的技术: 当编写并运行一个Java程序时,就同时体验了这四种技术.运行流程如下: Java虚拟机的主要任务是装载class文件并且执行其中的字节码.Java虚拟机包含 ...

  4. 无法启动程序 ”*.lib”

    解决办法: 把含有入口函数(main函数)的 工程 如 cpp-test 设置为启动项 具体操作: 选中 cpp-test 工程 右击 —> 设为启动项目

  5. Intent.putExtra()传递Object对象或者ArrayList<Object> (转)

    Intent传递基本类型相信大家都十分熟悉,如何传递Object对象或者ArrayList<Object>对象呢? 可以通过: (1)public Intent putExtra (Str ...

  6. sql 数字转人民币大写函数(两种方法)

    ,)) returns @rmb table( 亿 ) ,仟万 ) ,佰万 ) ,拾万 ) ,万 ) ,仟 ) ,佰 ) ,拾 ) ,元 ) ,角 ) ,分 )) as begin insert in ...

  7. hbase伪分布式安装(转)

    原文地址:http://blog.csdn.net/yonghutwo/article/details/24555103 本机环境: ubuntu 12.4 Hadoop 1.1.2 安装hbase版 ...

  8. Confluent Platform 3.0支持使用Kafka Streams实现实时的数据处理(最新版已经是3.1了,支持kafka0.10了)

    来自 Confluent 的 Confluent Platform 3.0 消息系统支持使用 Kafka Streams 实现实时的数据处理,这家公司也是在背后支撑 Apache Kafka 消息框架 ...

  9. Android 编程下 ListView 的 HeaderView 和 FooterView 不可选择点击

    在 ListView 里,HeaderView 和 FooterView 也占一行,与其他的 item 一样,可以点击,有索引,HeaderView 的索引为0.如果要使这两项不可点击,可以使用下面的 ...

  10. SpringRMI解析4-客户端实现

    根据客户端配置文件,锁定入口类为RMIProxyFactoryBean,同样根据类的层次结构查找入口函数. <bean id="rmiServiceProxy" class= ...