Hibernate + spring +struts + mysql乱码终极解决方案
时间:2011-01-23 httplei
(1)对mysql的my.ini配置文件的默认编码(好像是default-char-set)utf8
重启mysql服务。
(2)web.xml配置过滤器,这个应该很简单,copy就OK了
Xml代码
<filter>
<filter-name>
SetWebApplicationCharacterEncoding
</filter-name>
<filter-class>*.*.SetEncodeFilter</filter-class><!--这里填写你的包-->
<init-param>
<param-name>defaultencoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>
SetWebApplicationCharacterEncoding
</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>
Set Web Application Character Encoding
</filter-name>
<filter-class>*.*.SetEncodeFilter</filter-class> <!--这里填写你的包-->
<init-param>
<param-name>defaultencoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>
Set Web Application Character Encoding
</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
字符串过滤类
Java代码
importjava.io.IOException;
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
publicclassSetEncodeFilterimplementsFilter{
protectedFilterConfigfilterConfig=null;
protectedStringdefaultEncoding=null;
publicvoidinit(FilterConfigarg0)throwsServletException{
this.filterConfig=arg0;
this.defaultEncoding=filterConfig.getInitParameter("defaultencoding");
}
publicvoiddoFilter(
ServletRequestrequest,
ServletResponseresponse,
FilterChainchain)
throwsIOException,ServletException{
request.setCharacterEncoding("UTF-8");
chain.doFilter(request,response);
}
publicvoiddestroy(){
this.defaultEncoding=null;
this.filterConfig=null;
}
}
|