自定义标签库(一)

       使用自定义标签库,我们可以提高WEB应用的模块性、复用性、科维护性等等。相信对于标准标签库和Struts提供的标签库,我们是可以使用自如了,但是对于如何针对自己的应用来定制标签库,可能还是不太了解。下面介绍一下如何设计自定义标签库:

1、标签库德调用过程
        只有熟悉了标签库在WEB容器中是怎么调用的,我们才能更好的理解标签库的用途,方法的描述,更快的熟悉类库,很快的设计标签库。
        当自定义标签库的开始标记被遇到的时候,WEB容器就初始化这个标签库对应的处理器(Handler),然后调用处理器的doStartTag方法。当结束标记被碰到时,处理器的doEndTag方法就会被调用,另外一些方法的调用时发生在处理器和tag交互的时候。为了实现一个标记处理器(Handler),你必须实现这三个阶段对应的方法,具体描述如下表:

 Tag Handler Type Methods
 Simple doStartTag、doEndTag、release
 Attributes doStartTag、doEndTag、set/getAttribute1...N
 Body(No interaction) doStartTag、doEndTag、release
 Body(Interaction) doStratTag、doEndTag、release、doInitBody、doAfterBody


      要创建一个新的处理器,你必须要继承TagSupport或者BodyTagSupport来做为处理器的基类。

2、标记库描述器(Tag Library Descriptor)
   标记库描述器是一个描述标签库的XML文档(TLD),一个TLD文档包含的信息主要是整个库的信息和每个标签的具体信息。它主要是被JSP容器用来验证标签。下面的这些TLD元素就是通常要被定义的:
<taglib>

<tlibversion> - The tag library's version
<jspversion> - The JSP specification version the tag library depends on
<shortname> - A simple default name that could be used by a JSP page authoring tool to create names with a mnemonic value; for example, shortname may be used as the preferred prefix value in taglib directives and/or to create prefixes for IDs.
<uri> - A URI that uniquely identifies the tag library
<info> - Descriptive information about the tag library
<tag>
...
</tag>
...

</taglib>
     下面是Struts html标签库的部分代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
   <tlibversion>1.2</tlibversion>
   <jspversion>1.1</jspversion>
   <shortname>html</shortname>
   <uri>http://struts.apache.org/tags-html</uri>
   <tag>
      <name>base</name>
      <tagclass>org.apache.struts.taglib.html.BaseTag</tagclass>
      <bodycontent>empty</bodycontent>
      <attribute>
         <name>target</name>
         <required>false</required>
         <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
         <name>server</name>
         <required>false</required>
         <rtexprvalue>true</rtexprvalue>
      </attribute>
   </tag>
   <!--
      ...
   -->
</taglib>
   其实在开发时可以直接把上面的恶代码拷过来,针对自己开发的标签来改改就可以了!(强烈建议)

3、下面就通过创建一个简单的标签(simple Tags)来熟悉整个设计过程
>先来设计Handler
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public SimpleTag extends Tag Support {
   public int doStartTag() throws JspException {
      try {
         pageContext.getOut().print("Hello.");
      } catch (Exception ex) {
         throw new JspTagException("SimpleTag: " +
            e.getMessage());
      }
      //由于简单的没有bady,所以返回SKIP_BODY
      return SKIP_BODY;
   }
   public int doEndTag() {
      return EVAL_PAGE;
   }
}

>TLD
<tag>
   <name>simple</name>
   <tagclass>SimpleTag</tagclass>
   <bodycontent>empty</bodycontent>
</tag>

>页面调用:<taglibName:simple />

(未完待续)
   

<tlibversion> - The tag library's version
<jspversion> - The JSP specification version the tag library depends on
<shortname> - A simple default name that could be used by a JSP page authoring tool to create names with a mnemonic value; for example, shortname may be used as the preferred prefix value in taglib directives and/or to create prefixes for IDs.
<uri> - A URI that uniquely identifies the tag library
<info> - Descriptive information about the tag library
<tag>
...
</tag>
...

</taglib>
     下面是Struts html标签库的部分代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
   <tlibversion>1.2</tlibversion>
   <jspversion>1.1</jspversion>
   <shortname>html</shortname>
   <uri>http://struts.apache.org/tags-html</uri>
   <tag>
      <name>base</name>
      <tagclass>org.apache.struts.taglib.html.BaseTag</tagclass>
      <bodycontent>empty</bodycontent>
      <attribute>
         <name>target</name>
         <required>false</required>
         <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
         <name>server</name>
         <required>false</required>
         <rtexprvalue>true</rtexprvalue>
      </attribute>
   </tag>
   <!--
      ...
   -->
</taglib>
   其实在开发时可以直接把上面的恶代码拷过来,针对自己开发的标签来改改就可以了!(强烈建议)

3、下面就通过创建一个简单的标签(simple Tags)来熟悉整个设计过程
>先来设计Handler
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public SimpleTag extends Tag Support {
   public int doStartTag() throws JspException {
      try {
         pageContext.getOut().print("Hello.");
      } catch (Exception ex) {
         throw new JspTagException("SimpleTag: " +
            e.getMessage());
      }
      //由于简单的没有bady,所以返回SKIP_BODY
      return SKIP_BODY;
   }
   public int doEndTag() {
      return EVAL_PAGE;
   }
}

>TLD
<tag>
   <name>simple</name>
   <tagclass>SimpleTag</tagclass>
   <bodycontent>empty</bodycontent>
</tag>

>页面调用:<taglibName:simple />

(未完待续)
   

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值