|
||||||||||||
|
|
| strust框架中java web应用的中文化问题解决 |
| 时间:22/04/2007 作者:网络 来源:网络 |
| 小提示→点这里把文章加入您的收藏夹,方便下次查看 |
| 设置文章字体大小:[大 中 小] |
.中文化的解决方案:在应用程序中,我们不采用在tomcat中配置过滤器的方法,因为那会影响程序的执行效率。mysql数据库默认字符集默认,也就是latin1,我们前面两年用老版本的mysql,没有设置默认字符集,现在升级到新版本,最好也别用,再说我现在研究的方法,是独立于数据库服务器,servlet容器,http服务器的,也就是考虑到虚拟主机的情况。方案1:读出数据库统一的进行数据编码,数据的写入不用处理,jdbc数据库的连接按一般方式,不加characterEncoding. 下面是出库的程序。
public List executeQuery(String command){ ArrayList list = new ArrayList(); CharConvertor cc = new CharConvertor(); Connection conn = null; Statement stmt = null; ResultSet rs = null; try{ conn = ConnectionPool.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(command); package cn.qhang.common; public int addArticle(ArticleForm article){ String title = article.getTitle(); String categoryId = article.getCategoryId(); String addTime = article.getAddTime(); String content = article.getContent(); String source = article.getSource();//文章来源,比如教务处阿 等等,字符编码装换 String picture = article.getPicture(); String adminId = article.getAdminId();//只是管理员的id号,不用字符编码转换 String status = article.getStatus(); String sql = "INSERT INTO articles(title,categoryId,addtime,content,source,picture," +"adminId,status)VALUES('"+ title +"','" + categoryId + "','" + addTime + "','" + content + "','" + source + "','" + picture + "','" + adminId + "','" + status + "')"; return (stmts.executeUpdate(sql)); //当然成功了就会返回“1“,or not "0" /* * 晕阿~这种sql语句太难构建了吧,不过还好,写好了一劳永逸 :) */ } String info1="中文数据信息"; String info = cc.c2e(info1); String sql="INSERT INTO form.clumn = '" + info +"'"; //下面是执行的操作,省略了 1.数据库的默认字符集,默认为latin1; 2.只是在数据库读出资料时,进行必要的资料编码转换 3.数据的写入分两种情况处理,从formbean中获取的数据不能进行编码。 特别说明 这样的应用程序还有一个弊端,就是表单参数没有通过验证返回时在表单显示时,中文信息会出现乱码,这点从原因分析中可以看到。这点也是我为了解决这个问题到后面要提出的,修改struts源码来解决的方案。 第二种方案 在前一种方案中,我们做了一个单向的数据重新编码,在下面这个方案中,通过改变数据库连接url的方式,把那次数据的重新编码也去掉; jdbc的连接参数,我就举两个,其他的需要的去mysl官方网站看文档;看下面这个jdbc数据库的连接url: jdbc:mysql://localhost/qhang?user=user&password=password //poolman.xml <datasource> struts中文解决的终极方案,国际化的解决方案,修改struts 国际化的根本是用unicode,国际化不光是界面的国际化,而且还必须要能构正确的在数据库中存取资料。 (我整理之后再发,这些资料是写在我们工作组的wiki上的,开发的时候随手写的 有点乱http://www.qhang.cn:8080/snipsnap/)。 |
|
上一篇:基于Lucene的Mp3检索器
下一篇:strust框架中java web应用的中文化问题解决 |
| 【返回】 【顶部】 【关闭】 |
| Copyright © 2005-2010 www.594k.com All Rights Reserved. |
| 版权所有:JAVA学习网
备案序号:皖ICP备06004238号 |