您的位置:首页 > 编程语言 > Java开发

Spring MVC参数传递中文乱码解决方案

2017-02-05 12:03 148 查看

Spring MVC参数传递中文乱码解决方案

Spring MVC参数传递中文乱码解决方案
概述

GET提交中文乱码解决方案

POST提交中文乱码解决方案

概述

中国特色社会主义乱码问题是我们经常会碰到的问题,解决的办法有很多,本文分别介绍了GET方式和POST方式中文乱码解决方案中一劳永逸的办法。

GET提交中文乱码解决方案

在乱码的Controller文件中采用下面的方法将编码转换成UTF-8

String str = new String(request.getParameter("参数名").getBytes("iso-8859-1"), "utf-8");


修改项目所在的Tomcat服务器中的server.xml文件



<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>


修改为:

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


对于Ajax请求的GET方式中文乱码问题用上述方法仍然能够解决。

POST提交中文乱码解决方案

在web.xml文件中添加下面的内容:

<!-- 解决POST提交中文乱码问题的过滤器,注意只能解决POST提交中文乱码的问题 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息