网上有很多类似的处理方法,必须一个一个去尝试,然后根据自己的实际情况做决定。
1 分类:jsp网页乱码 、action 编写乱码、mysql数据库乱码
2 分别对应解决方案:
1) jsp网页乱码:<%@ page language = "java" contentType = "text/html; charset=UTF-8" %>
还有可能是tomcat的问题:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0"
disableUploadTimeout="true" useBodyEncodingForURI="true" URIEncoding="utf-8"
/>对比进行修改,是防止uri乱码
2)action servlet 编码问题:
action默认是iso-8859-1,必须转换成utf-8
string regname=new string( s.getBytes("iso-8859-1"),"utf-8");
3) struts编码过滤器:在web.xml下添加:
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果不行,在src下创建struts.properties,
内容设置 :struts.i18n.encoding=gb2312
如果不行,在struts.xml下添加(注意,这行一定要写在<struts></struts>中间的第一行)
<constant name="struts.i18n.encoding" value="utf-8" />
4)最后是数据库问题:
进入mysql数据库安装目录,看my.ini,将其中的编码改成utf-8
如果以上都做了的话,还是没有解决问题,可能需要在连接数据库的时候进行转码。school?useUnicode=true&characterEncoding=UTF-8