Hibernate + spring +struts + mysql乱码终极办理策划[Java编程]
本文“Hibernate + spring +struts + mysql乱码终极办理策划[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
(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;
}
}
以上是“Hibernate + spring +struts + mysql乱码终极办理策划[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |