软件编程
位置:首页>> 软件编程>> java编程>> Spring MVC参数传递中文乱码解决方法分享

Spring MVC参数传递中文乱码解决方法分享

作者:朱君鹏  发布时间:2023-05-11 02:10:34 

标签:spring,mvc,中文乱码

概述

中国特色社会主义乱码问题是我们经常会碰到的问题,解决的办法有很多,本文分别介绍了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>

总结

Spring SpringMVC在启动完成后执行方法源码解析

SpringMVC * 实现监听session是否过期详解

SpringMVC开发restful API之用户查询代码详解

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

来源:http://blog.csdn.net/jpzhu16/article/details/54880450#post提交中文乱码解决方案

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com