最近用structs2.3 + mysql5.5+tomcat6开发个小项目的时候,碰到了中文乱码的问题,现象,jsp页面的编码默认设为GB18030,mysql安装的时候设为gbk,tomcat没有动,后来发现,从jsp页面传到action中,中文全都变成"???",很是头疼,google了一下,用了网上说的,页面和配置文件编码都调成一致,也不行,在structs.xml中配置,写道
也不行。
后来,一步的研究,在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>
也不行,加上了spring的几个包,用spring的过滤器 <filter>
<filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
还是不行。
包括改tomcat的配置文件
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="GBK" />
还是不行,快绝望了。
辛辛苦苦的项目不能死在这啊,后来,自己参考网上的方法,思路,写了个过滤器,在web.xml中把其他字符过滤器全删了
package com.shasha.base;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
配置到web.xml中 <filter>
<filter-name>Encoding</filter-name>
<filter-class>
com.shasha.base.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
<!--gbk或者gb2312,GB18030或者utf-8-->
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
并且,把Tomcat,conf目录下面的server.xml中 <Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" redirectPort="8443" URIEncoding="GBK" />
增加 URIEncoding="GBK",发现,传入的action中中文,不是乱码了,很是欣喜,通过jdbc插入到数据库后,发现,在mysql中
还还是乱码,估计现在问题出现在了数据库,但是我的数据库安装的时候是GBK编码的,直接在数据库中插入中文,发现没问题。那么问题就应出现在jdbc上了,又在连接串上加了"&useUnicode=true&characterEncoding=UTF-8",后,重启,试了试,ok了,插入、查询中文都正常了。暂且这样解决吧。有时间再仔细研究下。