服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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

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

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

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


        
        
        

        
        
        
        
    

        
        
        
        
        

        
        


         testitem english : 

    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两个系统下都可以正常显示中文了,绝对没有问题。