文选流氓 发表于 2003-5-12 21:51

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

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

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

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

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

源码 (.tld文件和.java文件)
=====
app.tld
=====
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
      PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
      "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.1</jsp-version>
    <short-name>Application Tag Library</short-name>
<tag>
      <name>write</name>
      <tag-class>cloudor.jspext.tag.WriteTag</tag-class>
      <body-content>emplty</body-content>
      <attribute>
            <name>filter</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
            <name>ignore</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
            <name>property</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
            <name>scope</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
            <name>decode</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
      </attribute>
    </tag>
</taglib>
=====
app.tld结束
=====

=====
cloudor.jspext.tag.WriteTag
=====
//: WriteTag.java

package cloudor.jspext.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;


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

public class WriteTag extends org.apache.struts.taglib.bean.WriteTag
{
    /* ========================================
      Properties
    ======================================== */
    /**
   * Charset that we use to encode. Default is <code>gb18030</code>.
   *
   */
    protected String encode = "gb18030";
    public String getEncode() { return (this.encode); }
    public void setEncode(String encode) { this.encode = encode; }

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


    /* ========================================
      Public Methods
    ======================================== */


    /**
   * Process the start tag.
   *
   * @exception JspException if a JSP exception has occurred
   */
    public int doStartTag() throws JspException

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

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

      // Print this property value to our output writer, suitably filtered
      String s = value.toString();
      String output = "";
      try
      {
            output = new String( s.getBytes(encode) , decode );
      }catch(Exception e)
      {
            output = s;
      }
      if (filter)
            ResponseUtils.write(pageContext, ResponseUtils.filter(output));
      else
            ResponseUtils.write(pageContext, output);

      // Continue processing this page
      return (SKIP_BODY);

    }


    /**
   * Release all allocated resources.
   */
    public void release()
    {
      super.release();
      encode="gb18030";
      decode="iso8859-1";
    }
}
///:~
=====
WriteTag.java文件结束
=====



--
上上下下浇着那条毛巾,然后,我打了个冷战.
页: [1]
查看完整版本: 1-1-16-6-18 SP里用tag输出javabean里的中文字符 (附源码)