struts2 中文乱码问题(转)

【问题描述】
二、字符集和编码设置

数据库采用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代码
  1. private String prepare(String value) {  
  2.         if (escape) {  
  3.             return TextUtils.htmlEncode(value,false);  
  4.          } else {  
  5.             return value;  
  6.          }  
  7.      }  
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代码
  1. private String prepare(String value) {  
  2.          String result = value;  
  3.         if (escape) {  
  4.              result = TextUtils.htmlEncode(result,false);  
  5.          }  
  6.         if (escapeJavaScript) {  
  7.              result = TextUtils.escapeJavaScript(result);  
  8.          }  
  9.         return result;  
  10.      }  
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代码
  1. public final static String htmlEncode(String s) {  
  2.        return htmlEncode(s, true);  
  3.     }  
  4.   
  5.    /**
  6.      * Escape html entity characters and high characters (eg "curvy" Word quotes).
  7.      * Note this method can also be used to encode XML.
  8.      * @param s the String to escape.
  9.      * @param encodeSpecialChars if true high characters will be encode other wise not.
  10.      * @return the escaped string
  11.      */  
  12.    public final static String htmlEncode(String s, boolean encodeSpecialChars) {  
  13.         s = noNull(s);  
  14.   
  15.         StringBuilder str = new StringBuilder();  
  16.   
  17.        for (int j = 0; j < s.length(); j++) {  
  18.            char c = s.charAt(j);  
  19.   
  20.            // encode standard ASCII characters into HTML entities where needed  
  21.            if (c < '\200') {  
  22.                switch (c) {  
  23.                case '"':  
  24.                     str.append("&quot;");  
  25.   
  26.                    break;  
  27.   
  28.                case '&':  
  29.                     str.append("&amp;");  
  30.   
  31.                    break;  
  32.   
  33.                case '<':  
  34.                     str.append("&lt;");  
  35.   
  36.                    break;  
  37.   
  38.                case '>':  
  39.                     str.append("&gt;");  
  40.   
  41.                    break;  
  42.   
  43.                default:  
  44.                     str.append(c);  
  45.                 }  
  46.             }  
  47.            // encode 'ugly' characters (ie Word "curvy" quotes etc)  
  48.            else if (encodeSpecialChars && (c < '\377')) {  
  49.                 String hexChars = "0123456789ABCDEF";  
  50.                int a = c % 16;  
  51.                int b = (c - a) / 16;  
  52.                 String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);  
  53.                 str.append("&#x" + hex + ";");  
  54.             }  
  55.            //add other characters back in - to handle charactersets  
  56.            //other than ascii  
  57.            else {  
  58.                 str.append(c);  
  59.             }  
  60.         }  
  61.   
  62.        return str.toString();  
  63.     }  
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代码
  1. else if (encodeSpecialChars && (c < '\377')) {  
  2.                  String hexChars = "0123456789ABCDEF";  
  3.                 int a = c % 16;  
  4.                 int b = (c - a) / 16;  
  5.                  String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);  
  6.                  str.append("&#x" + hex + ";");  
  7.              }  
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代码
  1. private String prepare(String value) {  
  2.         if (escape) {  
  3.             return TextUtils.htmlEncode(new String(value.getBytes("iso-8859-1"),"gbk"));  
  4.          } else {  
  5.             return value;  
  6.          }  
  7.      }  
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代码
  1. private String prepare(String value) {  
  2.          String result = value;  
  3.         if (escape) {  
  4.              result = TextUtils.htmlEncode(new String(result.getBytes("iso-8859-1"),"gbk"));  
  5.          }  
  6.         if (escapeJavaScript) {  
  7.              result = TextUtils.escapeJavaScript(result);  
  8.          }  
  9.         return result;  
  10.      }  
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值