Spring 配置请求过滤器,编码格式设为UTF-8,避免中文乱码
<!-- 配置请求过滤器,编码格式设为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);
}
}
该字符集过滤器有两个重要参数,分别是encoding和forceEncoding
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()的作用是:同时设置ServletResponse和ServletRequest的编码格式。
配置相当于代码中:
resp.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");
在请求处理的过程中我们可以不用考虑编码方面的问题,上面两句代码可以省略,编码统一交给Spring过滤器去处理。
Spring 配置请求过滤器,编码格式设为UTF-8,避免中文乱码的更多相关文章
- Servlet中通过过滤器实现统一的手动编码(解决中文乱码)
1.web.xml片段: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi= ...
- 彻底解决Spring MVC 中文乱码 问题
1:表单提交controller获得中文参数后乱码解决方案 注意: jsp页面编码设置为UTF-8 form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果 <%@ p ...
- spring mvc中关于url中传递中文乱码的解决方法
在传值过程中,也是乱码出现的频繁地.先不说到底是什么场景了,通常常用的方案有如下几个 配置指定的filter <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> < ...
- ajax请求参数为中文乱码的情况
解决中文乱码问题的方法有很多. 一.前提是ajax请求传递参数对象到后台,对象中的某个参数的值为中文,到后台之后出现乱码,导致报错.问题解决如下: rest层: 二.在tomcat的server.xm ...
- spring boot 中文乱码问题
在刚接触spring boot 2.0的时候,遇到了一些中文乱码的问题,网上找了一些解决方法. 这里自己做个汇总. 在application.properties文件中添加: spring.http. ...
- SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理
SpringMvc Controller请求链接忽略大小写(包含拦截器)及@ResponseBody返回String中文乱码处理... @RequestMapping(value = "/t ...
- 0022SpringMVC解决post请求中文乱码的问题
我们在页面难免提交一些中文数据给后台处理,但是发现后台拿到的数据乱码,可以在每一个controller中都设置编码,但是太过于麻烦, 正确的解决办法应该是在web.xml中配置解决中文乱码的过滤器: ...
- 关于web.xml中配置Spring字符编码过滤器以解决中文乱码的问题
当出现中文乱码问题,Spring中可以利用CharacterEncodingFilter过滤器解决,如下代码所示: <!-- Spring字符编码过滤器:解决中文乱码问题 --> < ...
- SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】
配置web.xml(struts2的过滤器.spring监听器.解决Hibernate延迟加载问题的过滤器.解决中文乱码的过滤器) <!-- 解决中文乱码问题 --> <filter ...
随机推荐
- Lambda收集器示例
Collectors常用方法 工厂方法 返回类型 作用 toSet Set 把流中所有项目收集到一个 Set,删除重复项 toList List 收集到一个 List 集合中 toCollection ...
- java线程安全单例
public class MySingleton { // 使用volatile关键字保其可见性 volatile private static MySingleton instance = null ...
- bootstrap-table页码ALL显示为NAN
在github上查阅找到的解决办法: https://github.com/wenzhixin/bootstrap-table/issues/435 页面部分: data-page-list=&quo ...
- SQLServer如何批量替换某一列中的某个字符串
我们在开发系统的时候经常会碰到类似如下这样的情况:比如我有一张数据表 假如我现在要把红圈中这列的的http://www.mylanqiu.com/ 这个字符串批量替换成mylanqiu 这个字符串,这 ...
- 大型网站系统与java中间件实践-阅读笔记
线程池 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, Bl ...
- 自己封装了的AlertController
一直觉得使用系统这个东西写起来特别麻烦,每次都要写一大推东西,还是重复的,今天抽了点时间自己重新封装了一下,解决了自己的强迫症...,不多说,直接上代码了. 1.自己定义了一个名为XBZ的UIAler ...
- ABAP术语-SAPNET
SAPNET 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/17/1109823.html SAPNet is the intranet p ...
- 在SQL Server中批量修改有规律列的定义
)=N'要修改的表名'; --修改所有以sl结尾的列名的小数位数为4位 select syscolumns.name into #t1 from syscolumns,systypes where s ...
- MySQL必知必会 读书笔记三:检索数据和数据排序
检索数据 SELECT语句 它的用途是从一个或多个表中检索信息. 为了使用SELECT检索表数据,必须至少给出两条信息--想选择什 么,以及从什么地方选择. 检索单个列 SELECT col_1 FR ...
- jQuery的简单函数
1. jQuery函数的基本语法: $(document).ready(function(){ //代码块: }) 2.window.onload()和$(document).ready()的区分: ...