萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 1318|回复: 0

1-1-16-6-18 SP里用tag输出javabean里的中文字符 (附源码)

[复制链接]
发表于 2003-5-12 21:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x
发信人: cloudor (String[] args), 信区: Java
标  题: JSP里用tag输出javabean里的中文字符 (附源码)
发信站: BBS 水木清华站 (Sat Sep 28 14:56:37 2002), 站内信件


题外话:如果你已经能象google那样用utf-8而不是什么狭隘
的gb2312/gbk/gb18030等charset在同一个页面毫无冲突显
示简体繁体阿拉伯语藏文等多种语言,那你可以跳过本文了!

本文是针对那些正常中文String显示在charset=gb2312/gbk/gb18030
的jsp而作的。

何为正常中文String?如果String中的所有中文字都是一个char来
表示,那就谓之“正常”,而我们要正确输出这些正常中文String,
就必须将之“不正常化”,这就都怪JspWriter只能正确输出ISO8859-1
字符集中的char(如果这个我说错了,请纠正),所以对于一个正常
中文String s,我们要把它new String(s.getBytes("gb18030"),"iso8859-1")
以后才可以正确输出。不用担心你的中文char被腰斩,JspWriter写
这些char时其实告诉浏览器它在输出byte,浏览器将会重新组合这些
所谓的“byte”,将那些被腰斩的中文char救活。

我不想频繁来回使用这些讨厌的new String()和getBytes(),于是
继承了struts的<bean:write>,写了一给自己用的tag。(strusts
的jar文件和源码可以从jakarta.apache.org获得)

  1. 使用方法
  2. 1. 配置taglib的一般步骤省略,不懂就找本书来看吧。
  3. 2. jsp里先声明taglib
  4.    <%@ taglib uri="/WEB-INF/app.tld"    prefix="app" %>
  5. 3. 使用:
  6.   <%-- 输出String类型的bean --%>
  7.   <app:write name="mybean"/>

  8.   <%-- 输出bean 的String类型的property --%>
  9.   <app:write name="mybean" property="field" />

  10.   <%-- 同上,过滤html敏感字符 --%>
  11.   <app:write name="mybean" property="field" filter="true" />

  12.   <%-- 使用指定的encode和decode,默认encode为gb18030,
  13.       默认decode为iso8859-1,详情看源码 --%>
  14.   <app:write name="mybean" property="field" encode="gb18030"
  15.        decode="iso8859-1" />

  16. 源码 (.tld文件和.java文件)
  17. =====
  18. app.tld
  19. =====
  20. <?xml version="1.0" encoding="ISO-8859-1" ?>
  21. <!DOCTYPE taglib
  22.         PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  23.         "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
  24. <taglib>
  25.     <tlib-version>1.0</tlib-version>
  26.     <jsp-version>1.1</jsp-version>
  27.     <short-name>Application Tag Library</short-name>
  28. <tag>
  29.         <name>write</name>
  30.         <tag-class>cloudor.jspext.tag.WriteTag</tag-class>
  31.         <body-content>emplty</body-content>
  32.         <attribute>
  33.             <name>filter</name>
  34.             <required>false</required>
  35.             <rtexprvalue>true</rtexprvalue>
  36.         </attribute>
  37.         <attribute>
  38.             <name>ignore</name>
  39.             <required>false</required>
  40.             <rtexprvalue>true</rtexprvalue>
  41.         </attribute>
  42.         <attribute>
  43.             <name>name</name>
  44.             <required>true</required>
  45.             <rtexprvalue>true</rtexprvalue>
  46.         </attribute>
  47.         <attribute>
  48.             <name>property</name>
  49.             <required>false</required>
  50.             <rtexprvalue>true</rtexprvalue>
  51.         </attribute>
  52.         <attribute>
  53.             <name>scope</name>
  54.             <required>false</required>
  55.             <rtexprvalue>true</rtexprvalue>
  56.         </attribute>
  57.         <attribute>
  58.             <name>decode</name>
  59.             <required>false</required>
  60.             <rtexprvalue>true</rtexprvalue>
  61.         </attribute>
  62.     </tag>
  63. </taglib>
  64. =====
  65. app.tld结束
  66. =====

  67. =====
  68. cloudor.jspext.tag.WriteTag
  69. =====
  70. //: WriteTag.java

  71. package cloudor.jspext.tag;

  72. import javax.servlet.jsp.JspException;
  73. import javax.servlet.jsp.PageContext;
  74. import javax.servlet.jsp.tagext.TagSupport;
  75. import org.apache.struts.util.RequestUtils;
  76. import org.apache.struts.util.ResponseUtils;


  77. /**
  78. * Inheritance of <code>org.apache.struts.taglib.bean.WriteTag</code>
  79. * to handle extra encoding and decoding functions.
  80. *
  81. * @author Cloudor Pu(cloudor@etang.com)
  82. */

  83. public class WriteTag extends org.apache.struts.taglib.bean.WriteTag
  84. {
  85.     /* ========================================
  86.         Properties
  87.     ======================================== */
  88.     /**
  89.      * Charset that we use to encode. Default is <code>gb18030</code>.
  90.      *
  91.      */
  92.     protected String encode = "gb18030";
  93.     public String getEncode() { return (this.encode); }
  94.     public void setEncode(String encode) { this.encode = encode; }

  95.     /**
  96.      * Charset that we use to decode. Default is <code>iso8859-1</code>.
  97.      */
  98.     protected String decode = "iso8859-1";
  99.     public String getDecode() { return (this.decode); }
  100.     public void setDecode(String decode) { this.decode = decode; }


  101.     /* ========================================
  102.         Public Methods
  103.     ======================================== */


  104.     /**
  105.      * Process the start tag.
  106.      *
  107.      * @exception JspException if a JSP exception has occurred
  108.      */
  109.     public int doStartTag() throws JspException

  110.         // Look up the requested bean (if necessary)
  111.         Object bean = null;
  112.         if (ignore)
  113.         {
  114.             if (RequestUtils.lookup(pageContext, name, scope) == null)
  115.                 return (SKIP_BODY);  // Nothing to output
  116.         }

  117.         // Look up the requested property value
  118.         Object value =
  119.             RequestUtils.lookup(pageContext, name, property, scope);
  120.         if (value == null)
  121.             return (SKIP_BODY);  // Nothing to output

  122.         // Print this property value to our output writer, suitably filtered
  123.         String s = value.toString();
  124.         String output = "";
  125.         try
  126.         {
  127.             output = new String( s.getBytes(encode) , decode );
  128.         }catch(Exception e)
  129.         {
  130.             output = s;
  131.         }
  132.         if (filter)
  133.             ResponseUtils.write(pageContext, ResponseUtils.filter(output));
  134.         else
  135.             ResponseUtils.write(pageContext, output);

  136.         // Continue processing this page
  137.         return (SKIP_BODY);

  138.     }


  139.     /**
  140.      * Release all allocated resources.
  141.      */
  142.     public void release()
  143.     {
  144.         super.release();
  145.         encode="gb18030";
  146.         decode="iso8859-1";
  147.     }
  148. }
  149. ///:~
  150. =====
  151. WriteTag.java文件结束
  152. =====

复制代码


--
上上下下浇着那条毛巾,然后,我打了个冷战.
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网

GMT+1, 2025-2-12 08:08 , Processed in 0.063584 second(s), 19 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表