By default, SpringMVC not high for Chinese support, it is prone to Chinese garbled.
garbled reasons web applications:
Tomcat default character set ISO-8859-1, belonging to the Western European character set does not support Chinese.
The core idea is to solve the garbage to convert ISO-8859-1 to UTF-8.
The Controller requests and responses need to set UTF-8 character set, perfect to solve the garbage problem.
Get garbled request: In the Tomcat server.xml configuration file to increase URLEncoding property.
Post request distortion: CharacterEncodingFilter arranged in web.xml.
Garbled response Response: Configuration message StringHttpMessageConverter converter.
(1) request to resolve get garbled
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
In Tomcat 8.0 or later default is to use UTF-8, so 8.0 or later may not need to configure.
(2) post request distortion solution
Add filters in web.xml
<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 > <-! intercepts all the URL of -> < url-pattern > / * </ url-pattern > </ filter-Mapping >
(3) to solve the Chinese garbled response
In applicationContext.xml file, add a converter.
<mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters> <!--StringHttpMessageConverter对响应中的文本消息进行转换--> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>test/html;charset=utf-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>