数据库采用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= ******
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 方法改为:
- private String prepare(String value) {
- if (escape) {
- return TextUtils.htmlEncode(value,false);
- } else {
- return value;
- }
- }
- 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 方法改为:
- private String prepare(String value) {
- String result = value;
- if (escape) {
- result = TextUtils.htmlEncode(result,false);
- }
- if (escapeJavaScript) {
- result = TextUtils.escapeJavaScript(result);
- }
- return result;
- }
- 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
- 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(""");
- break;
- case '&':
- str.append("&");
- break;
- case '<':
- str.append("<");
- break;
- case '>':
- str.append(">");
- 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();
- }
- 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(""");
- break;
- case '&':
- str.append("&");
- break;
- case '<':
- str.append("<");
- break;
- case '>':
- str.append(">");
- 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 和现代希腊语字符。
现在我想大家一定已经都很明白了,为什么修改后的代码:
- 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 + ";");
- }
- 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 中的
- private String prepare(String value) {
- if (escape) {
- return TextUtils.htmlEncode(new String(value.getBytes("iso-8859-1"),"gbk"));
- } else {
- return value;
- }
- }
- 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 中的
- 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;
- }
- 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
<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();
}