说明:
(1)本篇博客必要性说明:默认情况下,Spring MVC对中文的支持度不高,很多时候会出现中文乱码的情况;本篇博客就是介绍中文乱码问题的解决策略;
(2)本篇博客的代码沿用自【SpringMVC入门与数据绑定9:Spring MVC数据绑定六:日期类型转换;(@DateTimeFormat注解;Converter转换器类;)】的【frist-springmvc项目】;
(3)本篇博客内容虽多,但实操起来并不复杂,需要时,及时查阅即可;
目录
一:解决【get请求的中文乱码】:设置Tomcat配置文件server.xml中的URIEncoding属性,配置UTF-8;
(1)需要配置Tomcat的server.xml配置文件中的URIEncoding;
二:解决【post请求的中文乱码】:在web.xml中配置CharacterEncodingFilter过滤器;
(0)没有设置前,Post请求乱码演示:启动Tomcat,效果如下:
(2)设置后,Post请求乱码演示:启动Tomcat,效果如下:
三:解决【响应的中文乱码】:在applicationContext.xml中配置StringHttpMessageConverter这个转换器;
(0)没有设置前,响应的中文乱码演示:启动Tomcat,效果如下:
(1)在Spring的配置文件applicationContext.xml中配置StringHTTPMessageConverter这个转换器;(核心内容!!!)
(2)设置后,响应的中文乱码演示:启动Tomcat,效果如下:
零:中文乱码简述;
(1) 中文乱码问题由来和解决思路;
(0)前面在介绍Servlet的时候,就接触过中文乱码的问题;
(1)中文乱码根源是字符集的问题;
● 计算机要识别某种自然语言,就必须依赖于字符集,不同的字符集使用不同的编码方式;字符集就像是一个【专为计算机使用的字典】,计算机通过特定的字符集把【指令】准换为【特定的字符】;
● Tomcat早期的版本中,使用ISO-8859-1字符集;这个字符集是西欧字符集,包含了英文字母、拉丁字母、数字、标准符号等;ISO-8859-1不支持中文;所以,使用ISO-8859-1字符集处理中文时,会出现乱码问题;
(2)解决策略;
● 核心策略就是把ISO-8859-1编码方式,转换为UTF-8的编码方式;
● UTF-8(Universal Character Set/Unicode Transformation Format)就是Unicode码的8位交换集格式;
● Unicode字符集,包含了世界上已知的各种语言,所以UTF-8自然支持中文;
(3)在开发中,请求和响应都需要设置UTF-8;
(2)具体的解决策略;
(0)基本上,在任何一个项目中,都需要设置以下三步;
(1)首先,解决get请求的乱码:需要在Tomcat的server.xml中增加URIEncoding属性,配置UTF-8;
(2)然后,解决post请求的乱码:在web.xml中配置CharacterEncodingFilter过滤器,通过Spring提供的这个过滤器,来解决post请求中的中文乱码问题;
(3)最后,解决响应的乱码:在Spring的配置文件applicationContext.xml中配置StringHTTPMessageConverter这个转换器;
……………………………………………………
如有需要可以快速参考:
● 最原始的Servlet中解决中文乱码问题: 【Servlet与JSP进阶七:Post请求中文乱码;Get请求中文乱码;响应中文乱码;】;
● 过滤器解决中文乱码问题:【过滤器四:案例-字符集过滤器;(设置请求与响应的编码,以过滤器的方式解决中文乱码)】;
● 第一次接触转换器的案例:【SpringMVC入门与数据绑定9:Spring MVC数据绑定六:日期类型转换;(@DateTimeFormat注解;Converter转换器类;)】
一:解决【get请求的中文乱码】:设置Tomcat配置文件server.xml中的URIEncoding属性,配置UTF-8;
(1)需要配置Tomcat的server.xml配置文件中的URIEncoding;
(2)启动Tomcat,观察效果;
二:解决【post请求的中文乱码】:在web.xml中配置CharacterEncodingFilter过滤器;
有关【使用过滤器解决请求和响应的中文乱码的问题】,如有需要可以快速参考: 【过滤器四:案例-字符集过滤器;(设置请求与响应的编码,以过滤器的方式解决中文乱码)】及附近文章;
(0)没有设置前,Post请求乱码演示:启动Tomcat,效果如下:
对于Spring MVC,为了解决post请求的中文乱码问题,目前广泛采用的方法是【增加一个过滤器,转换字符集】;
(1)Spring MVC中配置过滤器;(核心内容!!!)
在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> <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> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>characterFilter</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> </filter> <filter-mapping> <filter-name>characterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>说明:
(1) 如有需要可以快速参考: 【过滤器四:案例-字符集过滤器;(设置请求与响应的编码,以过滤器的方式解决中文乱码)】及附近文章;
(2)只是这儿Spring MVC已经帮我们写好了过滤器类,我们拿来用就行了;
(2)设置后,Post请求乱码演示:启动Tomcat,效果如下:
三:解决【响应的中文乱码】:在applicationContext.xml中配置StringHttpMessageConverter这个转换器;
(0)没有设置前,响应的中文乱码演示:启动Tomcat,效果如下:
对于Spring MVC,为了解决响应的中文乱码问题,目前广泛采用的方法是【在Spring的配置文件applicationContext.xml中配置StringHTTPMessageConverter这个转换器】;
(1)在Spring的配置文件applicationContext.xml中配置StringHTTPMessageConverter这个转换器;(核心内容!!!)
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.imooc.springmvc及子包中 拥有以下注解的对象. @Repository @Service @Controller @Component --> <context:component-scan base-package="com.imooc.springmvc"></context:component-scan> <mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=utf-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <mvc:default-servlet-handler/> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.imooc.springmvc.converter.MyDateConverter"/> </set> </property> </bean> </beans>说明:
(1)配置了哪些内容:
(2)配置内容说明:
(3)可以看到,这儿的套路,和以前在Controller中使用如【response.setContentType("text/html;charset=utf-8")】原理是一样的;只是,这儿我们是在配置文件中配置的,而这会让程序更加灵活和易于维护;
(2)设置后,响应的中文乱码演示:启动Tomcat,效果如下:
本文详细介绍了Spring MVC中遇到的中文乱码问题及其解决策略。针对GET、POST请求以及响应的中文乱码,分别提出了解决方案。GET请求的乱码通过在Tomcat的server.xml中配置URIEncoding为UTF-8解决;POST请求的乱码通过在web.xml中配置CharacterEncodingFilter过滤器来转换字符集;响应的乱码则通过在applicationContext.xml中配置StringHttpMessageConverter转换器,确保响应内容以UTF-8编码。通过这些配置,可以有效避免Spring MVC项目中的中文乱码问题。













被折叠的 条评论
为什么被折叠?



