springmvc web.xml文件配置中文编码过滤器
在web.xml文件中,加入以下配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!--中文编码过滤器配置--><filter><filter-name>encode</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>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encode</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>
查看源码
三个成员变量:
encoding 编码
forceRequestEncoding 默认为false
forceResponseEncoding 默认为false
public class CharacterEncodingFilter extends OncePerRequestFilter {@Nullableprivate String encoding;private boolean forceRequestEncoding;private boolean forceResponseEncoding;public CharacterEncodingFilter() {this.forceRequestEncoding = false;this.forceResponseEncoding = false;}
当设置为true时,分别对请求参数和响应参数进行编码
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {String encoding = this.getEncoding();if (encoding != null) {if (this.isForceRequestEncoding() || request.getCharacterEncoding() == null) {request.setCharacterEncoding(encoding);}if (this.isForceResponseEncoding()) {response.setCharacterEncoding(encoding);}}filterChain.doFilter(request, response);}
springmvc web.xml文件配置中文编码过滤器相关推荐
- 关于Servlet的两种配置Web.xml文件配置或者使用(@WebServlet(name = ,urlPatterns = ))配置问题——WebServlet注解
在Servlet2.5规范之前,Java Web应用的绝大部分组件都通过web.xml文件来配置管理. Servlet3.0规范可通过Annotation来配置管理Web组件,因此web.xml文件可 ...
- spring +springmvc+mybatis组合web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- web.xml文件中可以配置哪些内容?
web.xml用于配置Web应用的相关信息,如:监听器(listener).过滤器(filter).Servlet.相关参数. 会话超时时间.错误页面等①配置Spring上下文加载监听器,加载Spri ...
- SpringSecurity使用 配置文件 和wen.xml 文件配置
目录 1.web.xml 文件配置 2.spring-security 普通 为使用自己创建的认证类 1.web.xml 文件配置 !-- 配置SpringSecurity的拦截器 -->&l ...
- web.xml过滤html,Servlet过滤器两种配置方法(注解、配置web.xml文件)
方法一(注解) /** * 使用注解标注过滤器 * @WebFilter将一个实现了javax.servlet.Filte接口的类定义为过滤器 * 属性filterName声明过滤器的名称,可选 * ...
- 请说明一下web.xml文件中可以配置哪些内容?
● 请说明一下web.xml文件中可以配置哪些内容? web.xml用于配置Web应用的相关信息,如:监听器(listener).过滤器(filter). Servlet.相关参数.会话超时时间.安全 ...
- Java Web的web.xml文件作用及基本配置(转)
其实web.xml就是asp.net的web.config一个道理. 说明: 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. web.xml文件是用来 ...
- java面试(二十五)--(1)redis为什么读写速率快性能好(2)说说web.xml文件中可以配置哪些内容(3)和的区别(4)扑克牌顺子
1. redis为什么读写速率快性能好? 1.Redis将数据存储在内存上,避免了频繁的IO操作 2.Redis其本身采用字典的数据结构,时间复杂度为O(1),且其采用渐进式的扩容手段 3.Redis ...
- 在web.xml中配置过滤器
在web.xml中配置过滤器 <filter> <filter-name>loginFilter</filter-name>//过滤器名称 <filter-c ...
最新文章
- STM32低功耗模式下GPIO如何配置最节能?
- 配置ubuntu - tftp server服务器步骤
- Elixir: 开发和发布Elixir库
- html中<pre>标签
- Oracle起步——Oracle 11g安装配置
- Spring声明式事务管理
- C#(.Net) 连接 ORACLE数据库
- DirectionalLayout线性布局
- android 前台服务自定义布局不显示_Android自定义LinearLayout布局显示不完整的解决方法...
- 软件用户体验非常好的前端设计
- 信息学奥赛C++语言: 螺旋方阵1
- ERROR: source database template1 is being accessed by other users解决方法
- css中的一些问题及解决方法
- placeholder—Jquery插件placeholder的使用方法
- 试卷自动生成工具使用说明
- 春天,盐城北大青鸟学子收获成长的季节
- 西安python招聘信息_中软国际(陕西西安)2017招聘启事
- 学习笔记---高等数学前置知识---数列、排列组合、解不等式
- 新媒体运营教程:AARRR模型之留存有关方法论
- 设计师:裸眼 3D 效果,你们客户端实现很难吗?