首页 服务器 编程 必备知识 搜索引擎 圩日手册
站内搜索
最近浏览
推荐文章
热文排行

高级:Struts彻底实践中文问题的解决方法


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

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

   以下是代码内容:
   首先是JSP页面的内容,最简化的一个form
<%@ page contentType="text/html; charset=gb2312" language="java" %> 
 
    TestItem Chinese : 


    注意,页面的字符集一定要定义成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两个系统下都可以正常显示中文了,绝对没有问题。
[wangjy17908]
添加时间:2007-12-26
版权所有(C)2005-2015