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. Composer使用中常见的问题

    安装了Composer后,运行 composer --version ,查看Composer的版本号.如果出现下面的提示,那么软件安装成功. Composer version 1.2.0 2016-0 ...

  2. Redis整合Spring结合使用缓存实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...

  3. DedeCMS模板文件不存在,无法解析文档! 问题定位方法

    生成静态的时候,经常会遇到“模板文件不存在,无法解析文 档!”的问题.很多朋友试过论坛里很多方法,都是针对某些人可以解决,某些人的问题依旧,为什么呢?其实问题很可能确实是多种多样的,表现结果却是一样, ...

  4. js作用域链与this

    this的绑定与function和对象的定义位置无关,是由函数调用时的执行环境所决定的. scope chain是由函数定义时的位置决定的与函数调用时的执行环境无关.

  5. SlickGrid example 6:Ajax加载

    Ajax加载.   代码:   <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Ty ...

  6. 提高 Linux 上 socket 性能

      http://www.cnblogs.com/luxf/archive/2010/06/13/1757662.html 基于Linux的Socket网络编程的性能优化   1 引言    随着In ...

  7. 用户 NT AUTHORITY\NETWORK SERVICE 登录失败

    Windows server 2003,2008 Web.Config 配置连接sql 使用 win身份验证时: 当连接sql server使用信任连接(参看Web.Config文件)时就会出这个错误 ...

  8. Easyui Layout Center 全屏方法扩展

    $.extend($.fn.layout.methods, { full : function (jq) { return jq.each(function () { var layout = $(t ...

  9. 2016年12月13日 星期二 --出埃及记 Exodus 21:8

    2016年12月13日 星期二 --出埃及记 Exodus 21:8 If she does not please the master who has selected her for himsel ...

  10. PHP CURL实现远程下载文件到本地

    <?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...