# 5 spring MVC请求中文乱码问题

Web应用的中文乱码由来?

  • Tomcat默认的使用字符集ISO-8859-1,属于西欧字符集,ISO-8859-1不支持中文
  • 解决乱码的核心思路是将ISO-8859-1 转换为 UTF-8
  • Controller中请求与响应都需要设置UTF-8字符集

# 导航

回到Spring MVC导航页

# 中文乱码的配置

  1. Get请求乱码 - server.xml增加URIEncoding属性 解决Get请求乱码,需要依托于Web容器,需要在tomcat安装目录server.xml中增加URIEncoding属性
  2. Post请求乱码 - web.xml中配置CharacterEncodingFilter这个过滤器
  3. Response响应乱码 - Spring配置StringHttpMessageConverter

# Get请求乱码

在tomcat安装目录./conf,找到server.xml打开,找到Connector节点,添加设置URIEncoding="UTF-8"就可以了
在tomcat8.0以后版本,默认就是UT-8,所以在8.0以后写URIEncoding="UTF-8"与不写是一样的,但是在tomcat8.0之前的版本是需要写的

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />
1
2
3

# Post请求中的乱码

Post请求乱码 - web.xml中配置CharacterEncodingFilter这个过滤器
web.xml中配置如下:

<!--org.springframework.web.filter.CharacterEncodingFilter:字符编码顾虑器:主要作用就是将post请求中的字符由ISO-8859-1转换为UTF-8编码,如果想让其生效,还得配置filter-mapping-->
    <filter>
        <filter-name>characterFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <!--encoding:这里是写死的-->
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <!--如果想让post中文字符不在乱码生效,还得配置filter-mapping-->
    <filter-mapping>
        <filter-name>characterFilter</filter-name>
        <!--/*代表对所有的URL来进行拦截,只有这样才能对每一个post中的字符都被成功的转换-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <!--
        DispatcherServlet是Spring MVC最核心的对象
        DispatcherServlet用于拦截Http请求,并根据请求的URL调用与之对应的Controller方法,来完成Http请求的处理
        -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--启动时的一些参数-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--在Web应用启动时自动创建Spring IOC容器,并初始化DispatcherServlet-->
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--/  代表拦截所有请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--org.springframework.web.filter.CharacterEncodingFilter:字符编码顾虑器:主要作用就是将post请求中的字符由ISO-8859-1转换为UTF-8编码,如果想让其生效,还得配置filter-mapping-->
    <filter>
        <filter-name>characterFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <!--encoding:这里是写死的-->
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <!--如果想让post中文字符不在乱码生效,还得配置filter-mapping-->
    <filter-mapping>
        <filter-name>characterFilter</filter-name>
        <!--/*代表对所有的URL来进行拦截,只有这样才能对每一个post中的字符都被成功的转换-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# Response响应乱码

需要在applicationContext.xml文件中配置:

<!--conversion-service="converterServer"是转换类-->
    <mvc:annotation-driven conversion-service="converterServer">
        <mvc:message-converters>
            <!--StringHttpMessageConverter:对http响应中的文本进行转换StringHttpMessageConverter-->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <!--spring mvc解决响应中的乱码:底层调用的也是servlet中response.setContentType("text/html;charset=utf-8")-->
                        <value>text/html;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
1
2
3
4
5
6
7
8
9
10
11
12
13

applicationContext.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--
context:component-scan 标签的作用:
在Spring IOC初始化过程中,自动创建并管理com.torey.springmvc包及子包中拥有以下注解的对象:
@Repository  、@Service 、@Controller  、@Component
    -->
    <context:component-scan base-package="com.torey.springmvc"></context:component-scan>
    <!--启用Spring MVC的注解开发模式-->
    <!--<mvc:annotation-driven/>-->
    <!--conversion-service="converterServer"是转换类-->
    <mvc:annotation-driven conversion-service="converterServer">
        <mvc:message-converters>
            <!--StringHttpMessageConverter:对http响应中的文本进行转换StringHttpMessageConverter-->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <!--spring mvc解决响应中的乱码:底层调用的也是servlet中response.setContentType("text/html;charset=utf-8")-->
                        <value>text/html;charset=utf-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--将图片/JS/CSS等静态资源排除在外,可提高执行效率-->
    <mvc:default-servlet-handler/>
    <!--转换类 id可以随便写;FormattingConversionServiceFactoryBean 就是用来通知spring mvc到底有哪些自定义的转换类;配置好后需要与 <mvc:annotation-driven conversion-service="converterServer"/>关联起来-->
    <bean id="converterServer" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.torey.springmvc.converter.MyDateConverter"/>
            </set>
        </property>
    </bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# 测试文件目录如下

测试文件目录

# 导航,上一页,下一页

4springMVC数据绑定
6springMVC响应输出结果

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者

# 种一棵树,最好的时间是十年前,其次是现在

立志用功,如种树然。方其根芽,犹未有干;及其有干,尚未有枝;枝而后叶,叶而后花、实。初种根时,只管栽培灌溉,勿作枝想,勿作实想。悬想何益?何不忘栽培之功,怕没有枝叶花实?