JSP struts2 url传参中文乱码解决办法
1.设置struts.xml
<constant value="UTF-8" />
或是设置struts.properties(我自己没试过)
struts.i18n.encoding=UTF-8
2.在web.xml添加编码过滤器
<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> <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>
上面用的是spring自带的,也可以自己写,核心语句就是request.setCharacterEncoding(this.encoding);
3.设置tomcat的配置
1和2都只能设置post方式提交数据的编码方式,如果url有中文的话就会有乱码,解决办法是在
tomcat的\conf\server文件的
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
加入 URIEncoding="UTF-8"。如果你正在使用eclipse,记得重启eclipse,把服务器的设置删除,重新设置。我就是因为没有这样做搞得配置文件没生效,调试了半天