一 解决中文乱码问题
1.1 设置前端编码格式
-
JSP页面 :
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
-
HTML页面:
<meta charset="UTF-8">
1.2 设置服务器编码格式
-
tomcat/conf/server.xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
1.3 设置SpringMVC的编码方式
-
在web.xml中配置SpringMVC编码过滤器的编码方式
<filter> <filter-name>EncodingFilter</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>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
1.4 控制台乱码
Tomcat 设置 VM optins : -Dfile.encoding=UTF-8
1.5 JSON传输乱码
@Controller默认是走视图解析器,因为json对象就是返回一个字符串,所以说我们如果使用了@Controller注解的话,那么我们就要在方法上写一个注解@ResponseBody这个注解
在springmvc的配置文件中添加一段消息StringHttpMessageConverter转换配置
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
标签:编码,中文,Spring,乱码,MVC,设置,注解,编码方式
From: https://www.cnblogs.com/wanwanmeixiangdao/p/16628853.html