struts的中文乱码
(1)前提
由于struts的action不再继承传统的webapi对象,也默认不存在request对象捕获参数,所以传统的中文乱码问题方法在struts中是不适用的。
(2)struts的中文问题
A:页面编码使用utf-8,struts默认编码仍旧是utf-8,请求提交使用post方法,此时页面不存在乱码问题。
B:页面编码使用utf-8,struts默认编码仍旧是utf-8,请求提交使用get方法,此时存在乱码问题。
此时,需要更改服务器的编码,添加URIEncoding="utf-8"解决乱码问题。
<span style="font-size:12px;color:#006600;">设置下 tomcat的默认编码格式,
在 tomcat 里面的 \conf\server.xml 文件里的
<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" /></span>
C:页面编码使用gb2312,struts默认编码仍旧是utf-8,请求提交使用post方法。此时存在乱码问题的。需要更改struts.xml,将struts默认编码改成gb2312。
<constant name="struts.i18n.encoding" value="gb2312"/>实现乱码的解决。(在struts.xml中以常量方式配置)。
D:页面编码使用gb2312,struts默认编码仍旧是utf-8,请求提交使用get方法。需要更改struts.xml,将struts默认编码改成gb2312。
<constant name="struts.i18n.encoding" value="gb2312"/>
但是仍旧需要更改服务器编码:URIEncoding="gb2312"。
(3)建议
A:jsp页面编码使用utf-8,struts编码也是默认utf-8。
B:form尽可能使用post方法
C:get请求中尽可能不传递中文。