<filter>2、JS页面上设置pageEncoding
<filter-name>SetCharacterEncoding</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>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<a target=_blank href="mailto:%@page language='java' import='java.util.*' pageEncoding='UTF-8'%">%@page language="java" import="java.util.*" pageEncoding="UTF-8"%</a>3、如果使用的是Ajax进行数据传输出现乱码,对Ajax传输的数据设置编码contentType:"application/x-www-form-urlencoded; charset=utf-8":
$.ajax({4、使用tomcat服务器的情况下,上面几点都设置好了还是乱码就去查看tomcat配置文件web.xml(路径:tomcat/conf/web.xml),设置tomcat的编码过滤器为UTF-8,配置位置如下:
url : url,
type : "POST",
dataType : "json",
async : false,
contentType:"application/x-www-form-urlencoded; charset=utf-8",
data : $("#communication_frm").serialize(),
success : function(result) {},
error : function() {}
});
<!-- A filter that sets character encoding that is used to decode -->5、tomecat服务器的配置文件中,设置编码过滤器前面有一行注释,parameters in a POST request,从我遇到的实际情况来看确实是这样的,这个过滤器只对post方式发送的请求中的数据有效,而对get请求中的参数是无效的。 get请求方式发送的中文数据(url拼接)在前面都设置好后还会乱码的话,进入tomcat的配置文件server.xml(路径:tomcat/conf/server.xml),给
<!-- parameters in a POST request -->
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<Connector executor="tomcatThreadPool"添加URI的编码属性:URIEncoding="UTF-8"即改为:
port="1522"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector executor="tomcatThreadPool"
port="1522"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
目前我所遇到的乱码情况用这几种方式都能解决,如果还有其他情况欢迎告知补充。