struts2构建网站的国际化问题解决

来源:互联网 发布:程序员很枯燥 编辑:程序博客网 时间:2024/04/29 18:43
一、字符集和编码设置

数据库采用8859-1的字符集存储。

jsp页面:
<%@ page language="java" contentType="text/html; charset=8859_1"%>


struts2的struts.properties设置:struts.i18n.encoding=8859_1

web.xml的过滤器也设置过。用org.springframework.web.filter.CharacterEncodingFilter过滤器。

二、现象

录入中文正常。

显示中文有的Struts2标签有乱码,有的标签正常显示。具体如下:
1、s:property标签通过设置escape="false"能正常显示中文,否则乱码。
property value="label" escape="false"/>
2、s:select s:checkboxlist标签能正常显示中文
3、s:textfield 标签接收中文显示乱码。


注:action里面直接用值栈的方式传递到jsp。没用用session,request。
在之前的一个struts1的项目中采用同样的数据库和设置方式,能正常显示和处理中文。
如果设置都换成utf-8编码则数据库中原有的数据还要改变编码,而还有另一个系统要用到这里面的数据,故不能采用。

我想难道是struts2标签有问题。

三、可能的解决方案
1. struts2的struts.properties设置:struts.i18n.encoding= ******

2. 使用的数据库的字符集: iso-8859-1 ,在处理java代码时,对所有的含中文 配置文件其编码都是iso-8859-1 ,
java 代码编译时 -encoding "ISO8859-1" 。这样在使用struts2 时 就出现了乱码,主要是 <s:property 标签,经过对struts2源码的分析该标签输出时默认对输出内容做了htmlEncode 操作。
解决该问题:
1 将默认的执行 htmlEncode 操作,改为对输出内容不进行htmlEncode操作:
struts2-core-2.0.6 中 :
package org.apache.struts2.components ;
Property.java 中的
private boolean escape = true;
改为:
private boolean escape = false;

其次,将prepare 方法改为:

Java代码
    private String prepare(String value) {  
            if (escape) {  
                return TextUtils.htmlEncode(value,false);  
             } else {  
                return value;  
             }  
         }   

Java代码 复制代码 收藏代码

    private String prepare(String value) {  
            if (escape) {  
                return TextUtils.htmlEncode(value,false);  
            } else {  
                return value;  
            }  
        }  



struts2-core-2.1.6 中:
package org.apache.struts2.components
将 Property.java 中的
private boolean escape = true;
改为:
private boolean escape = false;

其次,将prepare 方法改为:
Java代码

    private String prepare(String value) {  
             String result = value;  
            if (escape) {  
                 result = TextUtils.htmlEncode(result,false);  
             }  
            if (escapeJavaScript) {  
                 result = TextUtils.escapeJavaScript(result);  
             }  
            return result;  
         }   

Java代码 复制代码 收藏代码

    private String prepare(String value) {  
         String result = value;  
            if (escape) {  
             result = TextUtils.htmlEncode(result,false);  
            }  
            if (escapeJavaScript) {  
             result = TextUtils.escapeJavaScript(result);  
            }  
            return result;  
        } 



再其次:
package org.apache.struts2.views.jsp;
将 PropertyTag.java 中的
private boolean escape = true;
改为:
private boolean escape = false;

这样就OK了~!

进一步说明: 对于需要使用 htmlEncode 的时候,那么就使用 <s:property 标签的 escape="true" (防止用户在输入内容时页面出现js注入错误)
但是以上的修改方法还是有一个问题:我们来看看xwork-2.1.2 中的
package com.opensymphony.xwork2.util;
TextUtils.java
Java代码
    public final static String htmlEncode(String s) {  
           return htmlEncode(s, true);  
        }  
      
       /**
         * Escape html entity characters and high characters (eg "curvy" Word quotes).
         * Note this method can also be used to encode XML.
         * @param s the String to escape.
         * @param encodeSpecialChars if true high characters will be encode other wise not.
         * @return the escaped string
         */  
       public final static String htmlEncode(String s, boolean encodeSpecialChars) {  
            s = noNull(s);  
      
            StringBuilder str = new StringBuilder();  
      
           for (int j = 0; j < s.length(); j++) {  
               char c = s.charAt(j);  
      
               // encode standard ASCII characters into HTML entities where needed  
               if (c < '\200') {  
                   switch (c) {  
                   case '"':  
                        str.append("&quot;");  
      
                       break;  
      
                   case '&':  
                        str.append("&amp;");  
      
                       break;  
      
                   case '<':  
                        str.append("&lt;");  
      
                       break;  
      
                   case '>':  
                        str.append("&gt;");  
      
                       break;  
      
                   default:  
                        str.append(c);  
                    }  
                }  
               // encode 'ugly' characters (ie Word "curvy" quotes etc)  
               else if (encodeSpecialChars && (c < '\377')) {  
                    String hexChars = "0123456789ABCDEF";  
                   int a = c % 16;  
                   int b = (c - a) / 16;  
                    String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);  
                    str.append("&#x" + hex + ";");  
                }  
               //add other characters back in - to handle charactersets  
               //other than ascii  
               else {  
                    str.append(c);  
                }  
            }  
      
           return str.toString();  
        }   

Java代码 复制代码 收藏代码

    public final static String htmlEncode(String s) {  
            return htmlEncode(s, true);  
        }  
      
        /**
         * Escape html entity characters and high characters (eg "curvy" Word quotes).
         * Note this method can also be used to encode XML.
         * @param s the String to escape.
         * @param encodeSpecialChars if true high characters will be encode other wise not.
         * @return the escaped string
         */  
        public final static String htmlEncode(String s, boolean encodeSpecialChars) {  
            s = noNull(s);  
      
            StringBuilder str = new StringBuilder();  
      
            for (int j = 0; j < s.length(); j++) {  
                char c = s.charAt(j);  
      
                // encode standard ASCII characters into HTML entities where needed  
                if (c < '\200') {  
                    switch (c) {  
                    case '"':  
                        str.append("&quot;");  
      
                        break;  
      
                    case '&':  
                        str.append("&amp;");  
      
                        break;  
      
                    case '<':  
                        str.append("&lt;");  
      
                        break;  
      
                    case '>':  
                        str.append("&gt;");  
      
                        break;  
      
                    default:  
                        str.append(c);  
                    }  
                }  
                // encode 'ugly' characters (ie Word "curvy" quotes etc)  
                else if (encodeSpecialChars && (c < '\377')) {  
                    String hexChars = "0123456789ABCDEF";  
                    int a = c % 16;  
                    int b = (c - a) / 16;  
                    String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);  
                    str.append("&#x" + hex + ";");  
                }  
                //add other characters back in - to handle charactersets  
                //other than ascii  
                else {  
                    str.append(c);  
                }  
            }  
      
            return str.toString();  
        } 



也就是说,我们即使 使用 <s:property 标签的 escape="true" 任然会有一定问题。这里我们首先复习一下:

ASCII 的表示内容如下:
0 – 31 控制符号
32 空格
33-47 常用符号
48-57 数字
58-64 符号
65-90 大写字母
91-96 符号
97-127 小写字母


ISO8859 如下:
编号 0 – 127 与 ASCII 保持兼容
编号128 – 159 共32个编码保留给扩充定义的 32 个扩充控制码
160 为空格
161 -255 的 95 个数字用于新增加的字符代码
编码的布局与 ASCII 的设计思想如出一辙,由于在一张码表中只能增加 95 种字符的代码,所以 ISO8859 实际上不是一张码表,而是一系列标准,包括 14 个字符码表。
例如,西欧的常用字符就包含在 ISO8859-1字符表中。在 ISO8859-7种则包含了 ASCII 和现代希腊语字符。


现在我想大家一定已经都很明白了,为什么修改后的代码:
Java代码
    else if (encodeSpecialChars && (c < '\377')) {  
                     String hexChars = "0123456789ABCDEF";  
                    int a = c % 16;  
                    int b = (c - a) / 16;  
                     String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);  
                     str.append("&#x" + hex + ";");  
                 }   

Java代码 复制代码 收藏代码

    else if (encodeSpecialChars && (c < '\377')) {  
                    String hexChars = "0123456789ABCDEF";  
                    int a = c % 16;  
                    int b = (c - a) / 16;  
                    String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);  
                    str.append("&#x" + hex + ";");  
                } 



才能将中文显示正确。


但是同时也是有隐患的,所以也就让我有了别的想法:
干脆先进行转码好了
struts2-core-2.0.6 中 :
package org.apache.struts2.components ;
Property.java 中的
Java代码
    private String prepare(String value) {  
            if (escape) {  
                return TextUtils.htmlEncode(new String(value.getBytes("iso-8859-1"),"gbk"));  
             } else {  
                return value;  
             }  
         }  

Java代码 复制代码 收藏代码
    private String prepare(String value) {  
            if (escape) {  
                return TextUtils.htmlEncode(new String(value.getBytes("iso-8859-1"),"gbk"));  
            } else {  
                return value;  
            }  
        } 


注:进行该修改 可不对
package org.apache.struts2.components
Property.java 中的
private boolean escape = true;
进行修改,让其默认进行 htmlEncode

struts2-core-2.1.6 中:
package org.apache.struts2.components
Property.java 中的
Java代码
    private String prepare(String value) {  
             String result = value;  
            if (escape) {  
                 result = TextUtils.htmlEncode(new String(result.getBytes("iso-8859-1"),"gbk"));  
             }  
            if (escapeJavaScript) {  
                 result = TextUtils.escapeJavaScript(result);  
             }  
            return result;  
         }  

Java代码
    private String prepare(String value) {  
         String result = value;  
            if (escape) {  
             result = TextUtils.htmlEncode(new String(result.getBytes("iso-8859-1"),"gbk"));  
            }  
            if (escapeJavaScript) {  
             result = TextUtils.escapeJavaScript(result);  
            }  
            return result;  
        } 


注:进行该修改 可不对:
package org.apache.struts2.components
Property.java 中的
private boolean escape = true;

package org.apache.struts2.views.jsp;
PropertyTag.java 中的
private boolean escape = true;
进行修改,让其默认进行 htmlEncode 操作,便可以显示正确的中文。

其他相关包说明:
xwork-2.0.1.jar
struts2-core-2.0.6.jar
struts2-spring-plugin-2.0.6.jar
struts2-tiles-plugin-2.0.6.jar

xwork-2.1.2.jar
struts2-core-2.1.6.jar
struts2-spring-plugin-2.1.6.jar
struts2-tiles-plugin-2.1.6.jar
0 0
原创粉丝点击