SpringMVC中文乱码总结

get方式

1.设置jsp页面为utf-8;

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

2.设置tomcat容器编码方案为utf-8,更改tomcat目录下conf中servlet.xml配置:

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

post方式提交:在web.xml中配置过滤器

<filter>
	<filter-name>encodingFilter</filter-name>
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filer-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>

**需要注意的是尽量使这段配置居于上部,由于xml解析文件顺序是从上而下解析,可能会出现拦截不到的情况


此外也可以仿照mvc的配置,在方法中加入编码:

response.setContentType("text/html;utf-8");
request.setCharacterEncoding("utf-8");


你可能感兴趣的:(spring,mvc,编码,乱码)