代码&优雅着&生活

导航

Spring MVC 3 表单中文提交post请求和get请求乱码问题的解决方法

在spring mvc 3.0 框架中,通过JSP页面、HTML页面以POST方式提交表单时,表单的参数传递到对应的servlet后会出现中文显示乱码的问题。解决办法可采用spring自带的过滤技术,对所有页面间参数的传递设置统一的字符编码。

对于post请求:

分两步解决问题:

1.设置页面格式为UTF-8

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 2.在web.xml中添加过滤器

<!--spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求 -->
    
     <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>  
      <init-param>  
       <param-name>forceEncoding</param-name>  
       <param-value>true</param-value>  
      </init-param>  
    </filter>  
    <filter-mapping>  
      <filter-name>characterEncodingFilter</filter-name>  
      <url-pattern>/*</url-pattern>  
    </filter-mapping> 

本文参考链接:http://blog.csdn.net/mybackup/article/details/7566590

 

对于get请求:

也是分两步解决:

1.同上1步骤。

2.在方法注解上设置produces值,如下:

@RequestMapping(value="/selectPlaceListMap",produces = "text/html;charset=UTF-8")

这样就解决了乱码问题。

 

posted on 2015-08-01 23:49  幸运的凌人  阅读(4763)  评论(0编辑  收藏  举报