HTTP的GET请求中文乱码

来源:互联网 发布:网游之演技一流张知 编辑:程序博客网 时间:2024/05/22 15:11

今日写完Controller的代码后,自测发现,参数若是中文,接收到的值为乱码。
最初以为是Spring容器的问题,百度后基本有以下几种解决方法:
1.web.xml中配置 CharacterEncodingFilter
2.把接收的参数转码 name = new String("name".getBytes("ISO-8859-1"), "UTF-8");
3.在<\mvc:annotation-driven/>中添加

<mvc:annotation-driven>    <mvc:message-converters register-defaults="true">         <!-- StringHttpMessageConverter编码为UTF-8,防止乱码 -->         <bean class="org.springframework.http.converter.StringHttpMessageConverter">             <constructor-arg value="UTF-8"/>         </bean>     </mvc:message-converters> <mvc:argument-resolvers> 

4.在RequestMapping中添加produces属性

@RequestMapping(value="one", method=RequestMethod.GET, produces="text/html;charset=utf-8")

实际测试发现,只有第二种有效,说明当前项目配置是没问题的。然后想到可能是容器的问题,查看ng配置后发现为UTF-8编码。于是查看tomcat配置,发现tomcat未设置编码,即使用默认的ISO-8859-1编码,改为UTF-8后,问题解决。

<Connector port="8019" protocol="HTTP/1.1" connectionTimeout="20000"     redirectPort="8443" URIEncoding="UTF-8"/>
0 0
原创粉丝点击