欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-16 09:26:58
概述 中国特色*乱码问题是我们经常会碰到的问题,解决的办法有很多,本文分别介绍了get方式和post方式中文乱码解决方案中一劳永逸的办法。 get提交中文乱码解决...

概述

中国特色*乱码问题是我们经常会碰到的问题,解决的办法有很多,本文分别介绍了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 mvc参数传递中文乱码解决方法分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

spring springmvc在启动完成后执行方法源码解析

springmvc拦截器实现监听session是否过期详解

springmvc开发restful api之用户查询代码详解

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

上一篇:

下一篇: