第七章:spring中的编码过滤器

在SpringMVC项目中,如果中文数据传递到后台出现乱码现象,说明需要配置一个过滤器,对传输的数据格式进行统一的转码。一般会在 web.xml 配置文件中设置SpringMVC的转码过滤器来解决这种问题:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6     <filter>
 7         <filter-name>CharacterEncodingFilter</filter-name>
 8         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 9         <init-param>
10             <param-name>encoding</param-name>
11             <param-value>UTF-8</param-value>
12         </init-param>
13     </filter>
14     <filter-mapping>
15         <filter-name>CharacterEncodingFilter</filter-name>
16         <url-pattern>/*</url-pattern>
17     </filter-mapping>
18 </web-app>

该配置表示,名为CharacterEncodingFilter的过滤器对所有请求进行过滤,然后该过滤器会以encoding参数所指定的编码格式对请求数据进行统一编码。

posted @ 2020-10-07 12:56  违和感  阅读(349)  评论(0)    收藏  举报