Spring字符编码过滤器
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配置CharacterEncodingFilter 时,可以配置两个参数:encoding和forceEncoding ;
encoding:编码格式;
forceEncoding :是否允许设置的encoding 覆盖request和response中已经存在的encodings。
- public class CharacterEncodingFilter extends OncePerRequestFilter {
- private String encoding;
- private boolean forceEncoding = false;
- /**
- * Set the encoding to use for requests. This encoding will be passed into a
- * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
- * <p>Whether this encoding will override existing request encodings
- * (and whether it will be applied as default response encoding as well)
- * depends on the {@link #setForceEncoding "forceEncoding"} flag.
- */
- public void setEncoding(String encoding) {
- this.encoding = encoding;
- }
- /**
- * Set whether the configured {@link #setEncoding encoding} of this filter
- * is supposed to override existing request and response encodings.
- * <p>Default is "false", i.e. do not modify 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 only be set on Servlet 2.4+
- * containers, since Servlet 2.3 did not provide a facility for setting
- * a default response encoding.
- */
- 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);
- }
- }
Spring字符编码过滤器的更多相关文章
- 关于web.xml中配置Spring字符编码过滤器以解决中文乱码的问题
当出现中文乱码问题,Spring中可以利用CharacterEncodingFilter过滤器解决,如下代码所示: <!-- Spring字符编码过滤器:解决中文乱码问题 --> < ...
- Spring的字符编码过滤器CharacterEncodingFilter
Spring中的字符编码过滤器,用来解决我们项目中遇到的编码问题. 使用方式特别友好,在web.xml加入: <filter> <description>字符集过滤器</ ...
- spring设置字符编码过滤器
一.在web.xml中的配置 <!-- characterEncodingFilter字符编码过滤器 --> <filter> <filter-name>chara ...
- SpringMVC配置字符编码过滤器CharacterEncodingFilter来解决表单乱码问题
1.GET请求 针对GET请求,可以配置服务器Tomcat的conf\server.xml文件,在其第一个<Connector>标签中,添加URIEncoding="UTF-8& ...
- 深入Struts2的过滤器FilterDispatcher--中文乱码及字符编码过滤器
引用 前几天在论坛上看到一篇帖子,是关于Struts2.0中文乱码的,楼主采用的是spring的字符编码过滤器(CharacterEncodingFilter)统一编码为GBK,前台提交表单数据到Ac ...
- Servlet字符编码过滤器,实现图书信息的添加功能,避免产生文字乱码现象的产生
同样的代码,网上可以找到和我一模一样的代码和配置,比我的更加详细,但是我重新写一个博客的原因自是把错误的原因写出来,因为这就是个坑,我弄了一天,希望对你们有所帮助.只为初学者发现错误不知道怎么解决有所 ...
- Jsp字符编码过滤器
通过此过滤器,可以实现统一将编码设置为UTF-8. 1.首先在web.xml中配置,添加如下代码: <!-- 过滤器 --> <filter> <filter-name& ...
- servlet中的字符编码过滤器的使用
一:简介 Servlet过滤器是客户端和目标资源的中间层组件,主要是用于拦截客户端的请求和响应信息.如当web容器收到一条客户端发来的请求 web容器判断该请求是否与过滤器相关联,如果相关联就交给过滤 ...
- Servlet字符编码过滤器
在Java Web程序开发中,由于Web容器内部使用编码格式并不支持中文字符集,所以,处理浏览器请求中的中文数据就会出现乱码的现象.由于Web容器使用了ISO-8859-1的编码格式,所以在Web应用 ...
随机推荐
- 125. Valid Palindrome(判断忽略标点的字符串是否回文,加个正则,与上一题解法一样)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- ThinkPHP框架基础知识三
一.JS文件与Css文件存放位置 其实JS与Css文件放在任意位置都可以找到,只要路径正确就行. 在TP框架中我们访问的所有文件都要走入口文件index.php,相当于访问的是index.php页面. ...
- python 利用PIL库进行更改图片大小的操作
python 是可以利用PIL库进行更改图片大小的操作的,当然一般情况下是不需要的,但是在一些特殊的利用场合,是需要改变图片的灰度或是大小等的操作的,其实用python更改图片的大小还是蛮简单的,只需 ...
- 20145201《Java程序设计》课程总结
每周读书笔记链接汇总 第一周读书笔记:http://www.cnblogs.com/20145201lzx/p/5249064.html 第二周读书笔记:http://www.cnblogs.com/ ...
- wyx20162314实验报告二
北京电子科技学院BESTI实验报告 课程:程序设计与数据结构 班级: 1623 姓名: 王译潇 学号:20162310 指导教师:娄佳鹏老师.王志强老师 实验日期:2017年3月26号 实验密级: 非 ...
- quartz(7)-源码分析
定时器启动 上图通过spring加载quartz <bean id="scheduler" class="org.springframework.schedulin ...
- eclipse显示结果窗口字体大小
设置前的字体大小 设置后的字体大小 步骤
- 使用nagios检测windows服务器
1.安装nagios windows客户端 下载NSClient++的安装包,注意根据实际的32/64CPU来下载 下载地址 http://sourceforge.net/projects/nscpl ...
- php。。。
我可能不会是一个合格的程序员,因为不够专一,学的种类多,精通的却很少,现在我要做为一个php程序员,专注起航了...接下来半年全力以赴,做出成绩吧. 另外之前的狂刷要一千题也要开始每天更新了,最难的就 ...
- RedHat 6.4企业版利用iso镜像做本地yum源
修改文章:http://linux.cn/article-1017-1.html 而RedHat的yum则需要注册付费才能使用,如果不这样则有两种解决方案 1. 利用iso镜像做本地yum源 2. 利 ...