JR 精品文章 - Struts彻底实践中文问题的解决方法
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 开发框架 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
Struts彻底实践中文问题的解决方法
agasi 原创   更新:2007-03-13 15:43:20  版本: 1.0   

   碰到struts中文问题时,在网上查了很多资料,想必碰到过此类问题的朋友也都查过,也都看到过差不多是同一篇文章。

   但是依法炮制了若干遍,JSP页面上仍然显示的是乱码,无奈,实践出真知,只好自己一遍一遍的试验,终于成功了,在windows的weblogic8下,和unix的weblogic8下均正确显示汉字。

   以下是代码内容:
   首先是JSP页面的内容,最简化的一个form
<%@ page contentType="text/html; charset=gb2312" language="java" %> 
<html> 
<body>
<table>
        <logic:iterate id="enid" name="box">
        <tr onMouseOver="this.style.backgroundColor='#D5D5D5'" onmouseout="this.style.backgroundColor='#FFFFFF'">
        <td>
        <html:multibox property="entrypro">
        <bean:write name="enid" />
        </html:multibox>
        <logic:iterate id="iid" name="enid">
        <td>
        <bean:write name="iid" />
        </td>
        </logic:iterate>
        <td></td>
        </tr>
        </logic:iterate>
    </table>
<html:form action="/testitem_config">
         TestItem English : <html:text property="testItemEn" /><br>
    TestItem Chinese : <html:text property="testItemCn" /><br>
    <br><br>
    <html:submit value="   Add  " /> 
</html:form>
</body>
</html>

    注意,页面的字符集一定要定义成gb2312,否则显示不了正确的汉字了,代码上半部的logic:iterate 标签是 action 处理完毕后,返回给页面显示的。至于form提交的部分,由struts机制去做了,我只把 testitem_config 这个 action 的原代码给出,大家就看明白了:


public class TestItemConfigAction  extends Action {
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response)throws Exception {
        
        TestItemConfigForm pcForm = (TestItemConfigForm)form;
        
        String[] entryIndexArray = pcForm.getEntrypro();
        
        String testPartKey;
        ArrayList testPartOptionsEn = new ArrayList();
        ArrayList testPartOptionsCn = new ArrayList();
        
        ServletContext context = getServlet().getServletContext();
        String file = context.getRealPath("/WEB-INF/classes/resource/TestItem.properties");
        PropertiesFileOperate pOperate = new PropertiesFileOperate(file);
        Properties property = pOperate.getProperties();
        int testpartnum = Integer.parseInt(property.getProperty("test.item.num"));
        
        if(pcForm.getOperateFlag() != null && !"".equals(pcForm.getOperateFlag())) {
    
            if(Integer.parseInt(pcForm.getOperateFlag()) == 1 && 
                    pcForm.getTestItemEn() != null && !"".equals(pcForm.getTestItemEn())){
                
                String addKeyEn = "test.item.en." + (testpartnum + 1);
                String addKeyCn = "test.item.cn." + (testpartnum + 1);
                String addValueEn = pcForm.getTestItemEn().trim();
                String addValueCn = pcForm.getTestItemCn().trim();

                String addValueCnWirite = new String(addValueCn.getBytes("ISO-8859-1"));

                pOperate.modifyProperties("test.item.num", (testpartnum + 1) + "");
                pOperate.addProperties(addKeyEn, addValueEn);
                pOperate.addProperties(addKeyCn, addValueCnWirite);
                pOperate.saveFile();
                pOperate = null;
            }
            
            if(Integer.parseInt(pcForm.getOperateFlag()) == 2 && 
                    pcForm.getTestItemEn() != null && !"".equals(pcForm.getTestItemEn())){
                int entryIndex = Integer.parseInt(pcForm.getTestItemIndex().trim());
                String addKeyEn = "test.item.en." + entryIndex;
                String addKeyCn = "test.item.cn." + entryIndex;
                
                String addValueEn = pcForm.getTestItemEn().trim();
                String addValueCn = pcForm.getTestItemCn().trim();
                
                String addValueCnWirite = new String(addValueCn.getBytes("ISO-8859-1"));
                
                pOperate.modifyProperties(addKeyEn, addValueEn);
                pOperate.modifyProperties(addKeyCn, addValueCnWirite);
                pOperate.saveFile();
                pOperate = null;
            }
            
            if(Integer.parseInt(pcForm.getOperateFlag()) == 3){
                for(int i = 0; i < entryIndexArray.length; i++){
                    String indexEntry = (entryIndexArray[i].substring(1, entryIndexArray[i].indexOf(","))).trim();
                    String addKeyEn = "test.item.en." + indexEntry;
                    String addKeyCn = "test.item.cn." + indexEntry;
                    
                    pOperate.modifyProperties(addKeyEn, "");
                    pOperate.modifyProperties(addKeyCn, "");
                }
                pOperate.saveFile();
                pOperate = null;
            }
        }
        
        PropertiesFileOperate pOperateShow = new PropertiesFileOperate(file);
        Properties propertyShow = pOperateShow.getProperties();
        
        int testpartNumber = Integer.parseInt(propertyShow.getProperty("test.item.num"));
        ArrayList array = new ArrayList();
        for(int i = 1; i <= testpartNumber; i++){
            ArrayList arr = new ArrayList();
            testPartKey = "test.item.en."+i;
            
            if (propertyShow.getProperty(testPartKey) != null && 
                    !"".equals(propertyShow.getProperty(testPartKey))){
                arr.add(i+"");
                
                testPartOptionsEn.add(propertyShow.getProperty(testPartKey));
                arr.add(propertyShow.getProperty(testPartKey));
                
                testPartKey = "test.item.cn."+i;
                testPartOptionsCn.add(new String(propertyShow.getProperty(testPartKey).getBytes(),"gb2312"));
                arr.add(propertyShow.getProperty(testPartKey));
                array.add(arr);
            }
        }

        request.setAttribute("box",array);
        
        pcForm.reset(mapping, request);
        return mapping.findForward("testitemone");
    }
}

    这个 action 并不复杂, 首先它定义了一个 properties 文件 TestItem.properties,在web服务器下的/WEB-INF/classes/resource/下面,用来记录页面上输入的内容,由于 String addValueCnWirite = new String(addValueCn.getBytes("ISO-8859-1")) 这个语句进行了字符转换,所以 properties 文件中记录的内容大概都是这样子的:
  test.item.cn.29=\u7F1D\u9699\u5F02\u5E38
  如果把程序改成记录到数据库中,也应该是这个样子,属于Unicode编码吧。
  而当要把记录的内容输出到客户端时候,new String(propertyShow.getProperty(testPartKey).getBytes(),"gb2312")) 这个语句又把Unicode编码转换成了GB2312,所以要求JSP页面charset=gb2312,呵呵,这样在windows 和 unix两个系统下都可以正常显示中文了,绝对没有问题。

   希望能够为大家节省一点开发实践,我试验这个东西用了8、9个小时呀。


版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     10       0
作者其它文章: 作者全部文章
评论人:zzxiaoma 发表时间: Thu Mar 22 16:26:50 CST 2007
确实很好
评论人:reciting 发表时间: Tue Apr 10 20:17:06 CST 2007
其实只需要在JSP页面上设置字符集为UTF-8,并在系统中增加一个Filter,并在重载的doFilter方法中将request的字符集改为UTF-8即可(默认是ISO8859),
  request.setCharacterEncoding(“UTF-8”);
这样可以显示各国文字,不仅是中文。
评论人:zuoguodang 发表时间: Thu Dec 20 21:16:40 CST 2007
好,楼主分析的太好,我前几天用到分页,把中文通过参数传过去,谁知道都是乱码,爆个NullPointerException,我都郁闷死啦

这个文章共有 3 条评论
主题: 《深入Spring2》终于开始发布电子版本了 上一篇文章
返回文章列表 返回〔开发框架〕
下一篇文章 主题: 公用Struts分页


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号