Spring字符集过滤器CharacterEncodingFilter
Spring中的字符集过滤器可以很方便的为我们解决项目中出现的中文乱码问题,而且使用方法也很简单,只需要在web.xml文件中配置一下该过滤器,设置两个重要的参数(encoding和forceEncoding)即可:
- <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
- <filter>
- <filter-name>springUtf8Encoding</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>springUtf8Encoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
以下是Spring字符集过滤器的源码:
- public class CharacterEncodingFilterextends OncePerRequestFilter {
- private String encoding;
- private boolean forceEncoding = false;
- /**
- * Set the encoding to usefor requests. This encoding will be passed into a
- * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
- * <p>Whether this encoding will overrideexisting request encodings
- * (and whether it will beapplied as default response encoding as well)
- * depends on the {@link #setForceEncoding "forceEncoding"} flag.
- */
- public void setEncoding(String encoding) {
- this.encoding = encoding;
- }
- /**
- * Set whether theconfigured {@link #setEncoding encoding} of this filter
- * is supposed to overrideexisting request and response encodings.
- * <p>Default is "false", i.e. do notmodify 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 onlybe set on Servlet 2.4+
- * containers, sinceServlet 2.3 did not provide a facility for setting
- * a default responseencoding.
- */
- 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);
- }
- }
由源码可以知道,该字符集过滤器有两个重要参数,分别是encoding和forceEncoding,这两个参数分别有什么作用呢?
以下是参考文档的介绍:
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还是response,encoding设置了两者的编码格式,只不过forceEncoding默认值为false,此时就只是设置了request的编码格式,即在Servlet中:
request.setCharacterEncoding("XXXX");
如果设置forceEncoding的值为true时,相当于Servlet中:
request.setCharacterEncoding("XXXX");
response.setCharacterEncoding(“XXXX”);
现在我们回过头来看看最初给大家看的web.xml中那部分过滤器的配置,相信大家都明白了,配置的作用相当于Servlet中的:
- @RequestMapping(value="XXXXX")
- public void XXXXX(User user,HttpServletRequestreq,HttpServletResponse resp) throws UnsupportedEncodingException
- {
- resp.setCharacterEncoding("UTF-8");
- req.setCharacterEncoding("UTF-8");
- ......
- }
因此,在请求处理的过程中我们可以不用考虑编码方面的问题,上面两句代码可以省略,编码统一交给Spring过滤器去处理,我们可以专心处理我们的业务逻辑代码,这就是Spring字符集过滤器的方便之处。
Spring字符集过滤器CharacterEncodingFilter的更多相关文章
- Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)
spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,Charact ...
- Spring的字符编码过滤器CharacterEncodingFilter
Spring中的字符编码过滤器,用来解决我们项目中遇到的编码问题. 使用方式特别友好,在web.xml加入: <filter> <description>字符集过滤器</ ...
- Spring编码过滤器:解决中文乱码
Spring编码过滤器:解决中文乱码 针对问题: 前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题: 解决方案: web.x ...
- 详解Spring中的CharacterEncodingFilter
在项目中有很多让人头疼的问题,其中,编码问题位列其一,那么在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?下面我们来看看Spring框架给我们提供过滤器CharacterEncodin ...
- Spring中的CharacterEncodingFilter
spring的配置文件如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=&q ...
- SpringMVC配置字符编码过滤器CharacterEncodingFilter来解决表单乱码问题
1.GET请求 针对GET请求,可以配置服务器Tomcat的conf\server.xml文件,在其第一个<Connector>标签中,添加URIEncoding="UTF-8& ...
- spring-security-4 (3)spring security过滤器的创建与注册原理
spring security是通过一个过滤器链来保护你的web应用安全.在spring security中,该过滤链的名称为springSecurityFilterChain,类型为FilterCh ...
- spring boot 过滤器、拦截器的区别与使用
原文:https://blog.csdn.net/heweimingming/article/details/79993591 拦截器与过滤器的区别: 1.过滤器和拦截器触发时机不一样,过滤器是在请求 ...
- CharacterEncodingFilter这个spring的过滤器
org.springframework.web.filter.CharacterEncodingFilter 对请求于响应的编码进行过滤,半路出家的和尚总是对什么都感觉到好奇,都想记录下来(
随机推荐
- Composer使用中常见的问题
安装了Composer后,运行 composer --version ,查看Composer的版本号.如果出现下面的提示,那么软件安装成功. Composer version 1.2.0 2016-0 ...
- Redis整合Spring结合使用缓存实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...
- DedeCMS模板文件不存在,无法解析文档! 问题定位方法
生成静态的时候,经常会遇到“模板文件不存在,无法解析文 档!”的问题.很多朋友试过论坛里很多方法,都是针对某些人可以解决,某些人的问题依旧,为什么呢?其实问题很可能确实是多种多样的,表现结果却是一样, ...
- js作用域链与this
this的绑定与function和对象的定义位置无关,是由函数调用时的执行环境所决定的. scope chain是由函数定义时的位置决定的与函数调用时的执行环境无关.
- SlickGrid example 6:Ajax加载
Ajax加载. 代码: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Ty ...
- 提高 Linux 上 socket 性能
http://www.cnblogs.com/luxf/archive/2010/06/13/1757662.html 基于Linux的Socket网络编程的性能优化 1 引言 随着In ...
- 用户 NT AUTHORITY\NETWORK SERVICE 登录失败
Windows server 2003,2008 Web.Config 配置连接sql 使用 win身份验证时: 当连接sql server使用信任连接(参看Web.Config文件)时就会出这个错误 ...
- Easyui Layout Center 全屏方法扩展
$.extend($.fn.layout.methods, { full : function (jq) { return jq.each(function () { var layout = $(t ...
- 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 ...
- PHP CURL实现远程下载文件到本地
<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...