struts2 的s:param标签中文乱码

二、字符集和编码设置 

数据库采用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.      }  
Java代码   收藏代码
  1. private String prepare(String value) {  
  2.         if (escape) {  
  3.             return TextUtils.htmlEncode(value,false);  
  4.         } else {  
  5.             return value;  
  6.         }  
  7.     }  


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.      }  
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.     }  


再其次: 
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.     }  
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.     }  


也就是说,我们即使 使用 <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.              }  
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.             }  


才能将中文显示正确。 


但是同时也是有隐患的,所以也就让我有了别的想法: 
干脆先进行转码好了 
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.      }  
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.     }  


注:进行该修改 可不对 
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.      }  
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.     }  


注:进行该修改 可不对: 
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



 <s:a action="manage_delete" namespace="/employee" ><s:param name="username" value="username"/>删除</s:a> 

 

 

String[] username=(String[]) ActionContext.getContext().getParameters().get("username");

System.out.println("action接受结果"+username[0]);

 

 

传递的参数出现乱码。尝试了各种方法,像

1、  在struts配置文件中设置:

 <constant name="struts.i18n.encoding" value="UTF-8" />

2、在web.xml文件中配置spring的过滤器


 <filter>
  <filter-name>setCharacterEncoding</filter-name>
  <filter-class>
   org.springframework.web.filter.CharacterEncodingFilter
  </filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>
 <!-- filtered type -->
 <filter-mapping>
  <filter-name>setCharacterEncoding</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

3、进行解码

 try {
   project_name=new String(project_name.getBytes("utf-8"));
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

这三种方式均不能解决问题。

一直以为是在这里用ognl表达式取出来的时候就乱码了,后来调了一天,想了想,才发现,这里执行完saveDir的action以后,转发方式是redirect,那么就相当于是在地址栏重新输入,下面这个action的地址了,然后加上后面的参数。那么就相当于你自己在地址栏输入listDirByParent.action?child.name=中文。。。,那么这样的请求属于get方式请求,不会通过过滤器进行过滤,而是通过tomcat进行地址的编码,而tomcat的编码,我记得是iso-8809-1,那么在action端Struts2默认是utf-8的编码方式,所以编码跟解码的方式出现了不符,就会出现乱码了。那么解决办法就是,直接在tomcat的server.xml中修改下面这段

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />其中加粗的是添加的内容。

修改完以后,ok了。

后来想到由上面的解决方法想到:如下方法也可以解决

 try {
   project_name=new String(project_name.getBytes("ISO-8859-1"),"utf-8");
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值