日期:2014-05-17  浏览次数:20788 次

HTMLDecoder,&#开头的编码换转成中文

转:http://blog.sina.com.cn/s/blog_4b4515650100ldf6.html

下面的几段代码是从《struts 2.1 权威指南》上摘的,因为比较好,就摘下来,方便以后查阅。

(1).这是主代码的图片。

HTMLDecoder,&#开头的编码换转成中文

public class HTMLDecoder {
?public static String decode(String str){
??String[] tmp = str.split(";&#|&#|;");
??StringBuffer sb = new StringBuffer("");
??for (int i=0; i<tmp.length; i ){
???if (tmp[i].matches("\\d{5 }")){
????sb.append((char)Integer.parseInt(tmp[i]));
???} else {
????sb.append(tmp[i]);
???}
??}
??return sb.toString();
?}
}
(2).这是一个页面,用于向Action中注入数据的,但不知道为什么,在TestAjaxAction.java中打印出的数据都是以“&#”开头的,所以要用到HTMLDecoder来处理一下。
HTMLDecoder,&#开头的编码换转成中文
(3).下面是一个调用HTMLDecoder.java的Action。
HTMLDecoder,&#开头的编码换转成中文