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

  1. <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
  2. <filter>
  3. <filter-name>springUtf8Encoding</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>springUtf8Encoding</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>

以下是Spring字符集过滤器的源码:

  1. public class CharacterEncodingFilterextends OncePerRequestFilter {
  2. private String encoding;
  3. private boolean forceEncoding = false;
  4. /**
  5. * Set the encoding to usefor requests. This encoding will be passed into a
  6. * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
  7. * <p>Whether this encoding will overrideexisting request encodings
  8. * (and whether it will beapplied as default response encoding as well)
  9. * depends on the {@link #setForceEncoding "forceEncoding"} flag.
  10. */
  11. public void setEncoding(String encoding) {
  12. this.encoding = encoding;
  13. }
  14. /**
  15. * Set whether theconfigured {@link #setEncoding encoding} of this filter
  16. * is supposed to overrideexisting request and response encodings.
  17. * <p>Default is "false", i.e. do notmodify the encoding if
  18. * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
  19. * returns a non-null value.Switch this to "true" to enforce the specified
  20. * encoding in any case,applying it as default response encoding as well.
  21. * <p>Note that the response encoding will onlybe set on Servlet 2.4+
  22. * containers, sinceServlet 2.3 did not provide a facility for setting
  23. * a default responseencoding.
  24. */
  25. public void setForceEncoding(boolean forceEncoding) {
  26. this.forceEncoding = forceEncoding;
  27. }
  28. @Override
  29. protected void doFilterInternal(
  30. HttpServletRequest request, HttpServletResponse response,FilterChain filterChain)
  31. throws ServletException, IOException {
  32. if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
  33. request.setCharacterEncoding(this.encoding);
  34. if (this.forceEncoding) {
  35. response.setCharacterEncoding(this.encoding);
  36. }
  37. }
  38. filterChain.doFilter(request, response);
  39. }
  40. }

由源码可以知道,该字符集过滤器有两个重要参数,分别是encodingforceEncoding,这两个参数分别有什么作用呢?

以下是参考文档的介绍:

setEncoding

public voidsetEncoding(java.lang.String encoding)

Set the encodingto use for requests. This encoding will be passed into aServletRequest.setCharacterEncoding(java.lang.String) call.

setForceEncoding

public voidsetForceEncoding(boolean forceEncoding)

Set whether theconfigured encoding of this filter is supposed to override existing request andresponse encodings.

通过参考文档,我们可以知道:

l  第一个方法setEncoding()相当于:ServletRequest.setCharacterEncoding(java.lang.String)

2. 第二个方法setForceEncoding()的作用是:

强制ServletResponse的编码格式和ServletRequest的编码格式一样。

也就是说,无论是request还是responseencoding设置了两者的编码格式,只不过forceEncoding默认值为false,此时就只是设置了request的编码格式,即在Servlet中:

request.setCharacterEncoding("XXXX");

如果设置forceEncoding的值为true时,相当于Servlet中:

request.setCharacterEncoding("XXXX");

response.setCharacterEncoding(“XXXX”);

现在我们回过头来看看最初给大家看的web.xml中那部分过滤器的配置,相信大家都明白了,配置的作用相当于Servlet中的:

  1. @RequestMapping(value="XXXXX")
  2. public void XXXXX(User user,HttpServletRequestreq,HttpServletResponse resp) throws UnsupportedEncodingException
  3. {
  4. resp.setCharacterEncoding("UTF-8");
  5. req.setCharacterEncoding("UTF-8");
  6. ......
  7. }

因此,在请求处理的过程中我们可以不用考虑编码方面的问题,上面两句代码可以省略,编码统一交给Spring过滤器去处理,我们可以专心处理我们的业务逻辑代码,这就是Spring字符集过滤器的方便之处。

Spring字符集过滤器CharacterEncodingFilter的更多相关文章

  1. Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)

    spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,Charact ...

  2. Spring的字符编码过滤器CharacterEncodingFilter

    Spring中的字符编码过滤器,用来解决我们项目中遇到的编码问题. 使用方式特别友好,在web.xml加入: <filter> <description>字符集过滤器</ ...

  3. Spring编码过滤器:解决中文乱码

    Spring编码过滤器:解决中文乱码 针对问题: 前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题: 解决方案: web.x ...

  4. 详解Spring中的CharacterEncodingFilter

    在项目中有很多让人头疼的问题,其中,编码问题位列其一,那么在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?下面我们来看看Spring框架给我们提供过滤器CharacterEncodin ...

  5. Spring中的CharacterEncodingFilter

    spring的配置文件如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=&q ...

  6. SpringMVC配置字符编码过滤器CharacterEncodingFilter来解决表单乱码问题

    1.GET请求 针对GET请求,可以配置服务器Tomcat的conf\server.xml文件,在其第一个<Connector>标签中,添加URIEncoding="UTF-8& ...

  7. spring-security-4 (3)spring security过滤器的创建与注册原理

    spring security是通过一个过滤器链来保护你的web应用安全.在spring security中,该过滤链的名称为springSecurityFilterChain,类型为FilterCh ...

  8. spring boot 过滤器、拦截器的区别与使用

    原文:https://blog.csdn.net/heweimingming/article/details/79993591 拦截器与过滤器的区别: 1.过滤器和拦截器触发时机不一样,过滤器是在请求 ...

  9. CharacterEncodingFilter这个spring的过滤器

    org.springframework.web.filter.CharacterEncodingFilter 对请求于响应的编码进行过滤,半路出家的和尚总是对什么都感觉到好奇,都想记录下来(

随机推荐

  1. jquery与服务器交换数据的利器--ajax(异步javascript and xml)

    load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 一.下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载 ...

  2. js 闭包理解

    闭包主要应用于两种情况: 1 函数作为返回值. 2 函数作为参数传递. 第一种举例: function fn(){ var max = 10; return function bar(){ if(x ...

  3. Java中的ClassLoader

      Java中类的加载过程(如Dog类):   通过类型信息定位Dog.class文件. 载入Dog.class文件,创建相应的Class对象. 执行父类的静态字段定义时初始化语句和父类的静态初始化块 ...

  4. Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...

  5. 山东理工大学第七届ACM校赛-最大收益问题 分类: 比赛 2015-06-26 10:25 51人阅读 评论(0) 收藏

    最大收益问题 Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 铁牌狗最近迷上了一款游戏,但铁牌狗实在是太笨了,他还是要请求你的帮助. 有 ...

  6. Java提高篇---Map总结

    一.Map概述 首先先看Map的结构示意图 Map:"键值"对映射的抽象接口.该映射不包括重复的键,一个键对应一个值. SortedMap:有序的键值对接口,继承Map接口. Na ...

  7. P2680 运输计划

    http://www.luogu.org/problem/show?pid=2680#sub 题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航 ...

  8. 使用jquery构建Metro style 返回顶部

    个人一直对metro风格的东西情有独钟,偶然间在腾讯网看到一款小插件,蓝色Metro风格的,所以决定把它放到我的博客中,这样做应该不会有版权问题吧orz.. Complete code 后言 我把他原 ...

  9. iq 格式分析

    po iq {type:1 name:iq xml:"<iq xmlns="jabber:client" to="testhjy@ecouser.net/ ...

  10. Python3基础 if-else实例 判断输入的数字是否为8

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...