Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)
Spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,CharacterEncodingFilter源代码如下:
/** Copyright 2002-2007 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.web.filter;import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** Servlet 2.3/2.4 Filter that allows one to specify a character encoding for* requests. This is useful because current browsers typically do not set a* character encoding even if specified in the HTML page or form.** <p>This filter can either apply its encoding if the request does not* already specify an encoding, or enforce this filter's encoding in any case* ("forceEncoding"="true"). In the latter case, the encoding will also be* applied as default response encoding on Servlet 2.4+ containers (although* this will usually be overridden by a full content type set in the view).** @author Juergen Hoeller* @since 15.03.2004* @see #setEncoding* @see #setForceEncoding* @see javax.servlet.http.HttpServletRequest#setCharacterEncoding* @see javax.servlet.http.HttpServletResponse#setCharacterEncoding*/
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;}@Overrideprotected 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);}}
上述代码显示,在配置字符集过滤器时可设定两个参数的值,如下:
l encoding:字符集,即将过滤到的request的字符集设置为encoding指定的值,如UTF-8等,相当于:
request.setCharacterEncoding
l forceEncoding:字面意思是强制字符集,但你大可不必按字面意思理解,因为这个参数的值只不过是指定response的字符集是否也设置成encoding所指定的字符集,所以你可以选择设置为true或false,当值为true时,相当于
request.setCharacterEncoding(“”);
response.setCharacterEncoding(“”);
当值为false时,相当于:
request.setCharacterEncoding(“”);
默认值为false。
示例:
<filter><filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
以上代码放置在web.xml中,相当于servlet中的:
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)相关推荐
- spring mvc字符编码过滤器 CharacterEncodingFilter ,添加例外url
前言 spring 4.3.4.RELEASE CharacterEncodingFilter : Spring MVC 提供的字符集过滤器,用于处理项目中的乱码问题 项目比较老,大部分url使用的是 ...
- Spring MVC 中 HandlerInterceptorAdapter过滤器的使用
一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的,这种方式可以实现Bean预处理.后处理. Spring MVC的拦截器不仅可实现Filter的所有功能,还可以更精确的控制拦截精度. ...
- Spring mvc 内置编码过滤器原理解析
在Spring mvc框架中是如何解决从页面传来的字符串的编码问题的呢? 下面我们来看看Spring框架给我们提供过滤器CharacterEncodingFilter. web.xml 中 添加如下配 ...
- Spring MVC【入门】就这一篇
MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者 JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Ja ...
- Spring MVC【入门】就这一篇!
MVC 设计概述 在早期 Java Web 的开发中,统一把显示层.控制层.数据层的操作全部交给 JSP 或者JavaBean 来进行处理,我们称之为 Model1: 出现的弊端: JSP 和 Jav ...
- Spring MVC 教程详解 个人总结 复习必备 面试宝典 狂神笔记
文章目录 一.MVC 模式 1.什么是 MVC 2.Servlet MVC 小结 二.Spring MVC 1.Spring MVC 概念 为什么学习 Spring MVC 中央控制器 Dispatc ...
- Spring MVC 函数式编程进阶
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | 码农小胖哥 1. 前言 上一篇对 Spring ...
- Spring MVC过滤器-HttpPutFormContentFilter
在Spring MVC过滤器-HiddenHttpMethodFilter中我们提到,jsp或者说html中的form的method值只能为post或get,我们可以通过HiddenHttpMetho ...
- Spring MVC过滤器-HiddenHttpMethodFilter
随时随地技术实战干货,获取项目源码.学习资料,请关注源代码社区公众号(ydmsq666) from:Spring MVC过滤器-HiddenHttpMethodFilter_Blake Luo-CSD ...
最新文章
- 不错的Android开发网站
- linux Shell(脚本)编程入门实例讲解详解
- c++初学者使用文件流需要了解的一些坑(持续更新)
- Apache Flink 零基础入门(十一)Flink transformation
- RocketMQ事务消息的三种状态
- 【转】SharePoint 2013 开发——开发并部署webpart
- 父子结构查询_Java面试准备(5)之数据结构与算法——红黑树
- SpringCloud使用RabbitMQ报错Rabbit health check failed
- C语言栈的面试题,[面试题]EMC易安信-C语言函数堆栈的思考
- 17.电话号码的字母组合(力扣leetcode) 博主可答疑该问题
- visual studio插件开发dll类库免加全局缓存处理办法
- java 中介模式_java设计模式-中介者模式
- 【Java定时器】每天凌晨12点执行一次
- 三维叉乘怎么算_小学数学心算速算:多位数乘一位数
- 锐目对讲机的使用方法详解
- 医依通小程序项目总结
- mysql格式化日期的函数_MySql格式化日期函数
- IT产品是计算机类产品吗,IT产品是什么
- pyecharts-map世界地图国家中英文对照表
- Luogu_P3258 松鼠的新家