免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 736 | 回复: 0
打印 上一主题 下一主题

Struts中的中文乱码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-20 19:31 |只看该作者 |倒序浏览

1、资源文件里读出来在页面上的乱码;
2、数据库读出来的乱码
3、数据库写进去的乱码
4、在ActionForm验证不通过Errors返回的乱码,也就是request,IE参数传递的乱码了。
5、有一个JS写的时间选择跳出框,如果在charset=UTF-8的时候就出错,但如果我改为charset=GBK或GB2312时就一切正常了
下面是我的解决方法:
1、资源文件里读出来在页面上的乱码:这个最容易解决了,把写好的ApplicationResources.properties文件,在DOC底下用 native2ascii -encoding gb2312 ApplicationResources.properties
ApplicationResources_zh_CN.properties 命令来个字符编码转换,将原来的中文转为Unicode编码就搞定了中文简体,繁体也用同样的命令,只是把 bg2312 改为 bgk 就可以了。
2、数据库读写的乱码,刚开始的时候因为受以前的SQL Server+JDBC 影响(在那时写入数据库是可以不用做什么工作的,只是在读出来的时候来个 gbk = new String(iso.getBytes("ISO-8859-1"), "GBK") 转换就行了)我也都在把读写都在转换,搞得好复杂也很麻烦,后来在连接池连接代码jdbc:mysql://localhost:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=GBK那里加上一个&useUnicode=true&characterEncoding=GBK,保证了在数据库操作时候使用了统一的编码字符集,又解决了两个乱码问题,一举两得,嘿嘿。
4、request,response的乱码在网上找了好久,也有两个解决的办法,也是来个转换,还有种办法是写一个过滤器,我选择了后者,因为简单:),这方法用到两个文件,一个是 filter ,一个是 web.xml 文件,代码在后面。
5、至于JS的这个问题,还没办法,只好在JSP页面上改为了,反正这样也没问题。
到此为止,乱码问题总算告一段落了,感觉蛮不错的,郁闷了这么久,终于可以高兴了好大一段子了。
-------------------------------------------------------------------------------------------------------------------
过滤器使用代码:EncodingFilter.java
package com.***.***.***;
import javax.servlet.*;
import java.io.IOException;
public class EncodingFilter implements Filter {
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
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);
}
}
//End
WEB.XML文件:将下面的代码放在WEB.xml文件的下面就行了
encodingFilter
EncodingFilter
Set the request encoding
com.voip.admin.util.EncodingFilter
encoding
GB18030
ignore
true
encodingFilter
/*


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/20879/showart_203785.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP