到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.javaresearch.org无关,不对该信息负责.通过http://www.javaresearch.org/article/64218.htm访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
Struts彻底实践中文问题的解决方法
作者: 来源:www.javaresearch.org  发布时间:2007-03-14 12:08:17.657


   碰到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个小时呀。


 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读