<!-- 配置请求过滤器,编码格式设为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>

注解配置:

@Bean
CharacterEncodingFilter characterEncodingFilter(){
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
return filter;
}

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);
}
}

该字符集过滤器有两个重要参数,分别是encodingforceEncoding

setEncoding

public void setEncoding(java.lang.String encoding)

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

setForceEncoding

public void setForceEncoding(boolean forceEncoding)

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

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

l.第一个方法setEncoding()相当于:ServletRequest.setCharacterEncoding(java.lang.String),即设置request编码格式

2.第二个方法setForceEncoding()的作用是:同时设置ServletResponseServletRequest的编码格式。

配置相当于代码中:

resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");

在请求处理的过程中我们可以不用考虑编码方面的问题,上面两句代码可以省略,编码统一交给Spring过滤器去处理。

Spring CharacterEncodingFilter的更多相关文章

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

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

  2. Spring字符集过滤器CharacterEncodingFilter

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

  3. 详解Spring中的CharacterEncodingFilter

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

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

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

  5. Spring中的CharacterEncodingFilter

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

  6. Spring 中 CharacterEncodingFilter 失效?

    # 问题 Spring 提供了CharcterEncodingFilter,专门解决字符串编码的问题. 诡异的是,在类 AbstractAnnotationConfigDispatcherServle ...

  7. 通过org.springframework.web.filter.CharacterEncodingFilter定义Spring web请求的编码

    通过类org.springframework.web.filter.CharacterEncodingFilter,定义request和response的编码.具体做法是,在web.xml中定义一个F ...

  8. CharacterEncodingFilter这个spring的过滤器

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

  9. 快速搭建springmvc+spring data jpa工程

    一.前言 这里简单讲述一下如何快速使用springmvc和spring data jpa搭建后台开发工程,并提供了一个简单的demo作为参考. 二.创建maven工程 http://www.cnblo ...

随机推荐

  1. html5学习笔记(3)--主题结构元素-1

    Article元素 以下为对应代码: <!DOCTYPE html> <html> <head lang="en"> <meta char ...

  2. C#删除程序自身【总结】

    偶然看到一个可以自删除的程序,于是了解下如何实现.然后整理如下: 思路: 在.NET程序中,因为运行中的程序是受系统保护的,不能自己删除自身的,所以自删除的思路:  在关闭本程序之前启动新的进程打开另 ...

  3. 孙鑫MFC学习笔记10:画图/贴图

    1.SetPixel在指定点设置像素 2.虚线.点线宽度必须为1 3.CColorDialog创建颜色对话框 4.需要设置CC_RGBINIT标志才能设置颜色对话框的默认颜色 5.CC_FULLOPE ...

  4. expect笔记

    #!/usr/bin/expect -f set ip [lindex $argv 0]; set password [lindex $argv 1];  set timeout 1 spawn ss ...

  5. inflate的基本用法,类似于findviewbyId

    Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能.最近做的一个项目就是这一点让我迷茫了好几天. android上还有一个与Inflate()类 ...

  6. ahjesus 单词单数-复数相互转换C#

    看codesmith内置的模板在生成存储过程的时候有单复数的转换,用相同的函数名实现了一个 public static class StringUtil { /// <summary> / ...

  7. tomcat 7下spring 4.x mvc集成websocket以及sockjs完全参考指南

    之所以sockjs会存在,说得不好听点,就是因为微软是个流氓,现在使用windows 7的系统仍然有近半,而windows 7默认自带的是ie 8,有些会自动更新到ie 9,但是大部分非IT用户其实都 ...

  8. 为友盟消息推送开发的PHP SDK(composer版):可以按省发Android push

    一直以来APP希望按省市县推送Android push,只能自己分析用户经纬度,打tag发送. 现在终于有服务商提供了. 友盟消息推送 可以“按省推送”,很方便. 我为友盟做了PHP SDK(comp ...

  9. Spring MVC 处理模型数据(@ModelAttribute)

    SpringMVC中的模型数据是非常重要的,因为MVC中的控制(C)请求处理业务逻辑来生成数据模型(M),而视图(V)就是为了渲染数据模型的数据. 直白来讲,上面这句话的意思就是:当有一个查询的请求, ...

  10. HTML中tr标签设置边框不显示的解决办法

    今天在操作表格的时候发现设置表格中行的边框没有显示,然后自己新建了一个表格发现确实不显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...