spring mvc form表单submit直接提交出现乱码。导致乱码一般是服务器端和页面之间编码不一致造成的。根据这一思路可以依次可以有以下方案。
1、jsp页面设置编码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
2、tomcat服务器设置,在server.xml中添加编码设置 URIEncoding="UTF-8"
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>
3、web.xml中添加编码设置
<filter> <filter-name>CharacterEncoding</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>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注意,此段编码设置必须在所有filter的前面,否则过滤器有可能不起作用。