标签扩展是一个JAVA类,它是运行一个或者二个接口的JAVABEAN。在JSP规范内标签具有比JAVABEAN更丰富的运行时协议,因为
1:初始化可以包含属性,没有这些属性标签就不能运行 ,而JAVABEAN的构造 函数 参数 为空
2:设置和获取属性不会涉及到所有的类,在BEAN中只有少量的逻辑控制。
3:JSP页面中的BEAN没有 默认设置上下文,也就是说,BEAN没有一个父类的或者页面上下文对象的默认概念。
JSP所有的标签都实现了javax.servlet.jsp.tagext.JspTag接口。这个接口是一个标记接口,它有二个子接口:
1:一个是SimpleTag,它是JSP2.0新增加的接口,代表了简单的标签。
2:第二个是Tag接口,它是经典的,必须实现的接口,它有一个直接子接口就是IterationTag
IterationTag用于开发出抚今追昔的标签,它有一个简单的实现类为TagSupport。在开发时,我们只要从TagSupport扩展就可以开发出抚今追昔标签了。IterationTag还有一个子接口,就是BodyTag,这种标签允许带有Body。BodyTag也有一个实现类,就是BodyTagSupport,在开发BodyTag时,往往直接从它继承就可以了。当然了,我们在开发自己的标签时除了继承原有的类外,也可以直实现Tag接口。

HelloWorld标签开发
传统的标签必须实现javax.servlet.jsp.tagext.Tag接口,在这个接口中,主要定义的是和标签声明周期相关的方法,比如doStartTag() ,doEndTag() 等。在Tag中,可以通过pageContext对象访问JSP页面 的上下文。下面结合标签的生命周期讨论下标签的处理过程。JSP1.2标签的生命周期如下
1:当容器创建一个新的标签实例后,通过setPageContext设置标签的页面上下文。
2:使用setParent方法设置这个标签的上一级标签。如果没有上一级嵌套,设置为空。
3:设置标签的属性。这个属性在标签库描述文件中定义。如果没有定义属性就不调用此类方法。
4:调用 doStartTag方法,这个方法可以返回EVAL_BODY_INCLUDE和SKIP_BODY。当返回EVAL_BODY_INCLUDE时,就计算标签的BODY,如果返回SKIP_BODY,就不计算标签的BODY。
5:调用doEndTag方法,这个方法可以返回EVAL_PAGE或者SKIP_PAGE。当返回EVAL_PAGE时,容器将在标签结束时继续计算JSP页面的其他部分;如果返回SKIP_PAGE,容器将在标签结束时停止计算JSP页面的其他部分。
6:调用release方法释放标签程序占用的任何资源。
在开发标签时,可以有两种选择,一种是直接实现原始接口,另一种是从TagSupport类继承。下面我使用二种不同的方式来写一个HelloWorld标签程序

一:实现Tag接口
1:开发实现类

自定义标签之HelloWorldpackage eflylab;
自定义标签之HelloWorld
自定义标签之HelloWorld
import javax.servlet.jsp.*;
自定义标签之HelloWorld
import javax.servlet.jsp.tagext.*;
自定义标签之HelloWorld
import java.util.Hashtable;
自定义标签之HelloWorld
import java.io.Writer;
自定义标签之HelloWorld
import java.io.IOException;
自定义标签之HelloWorld
import java.util.Date;       
自定义标签之HelloWorld
2:编写标签库描述(mytag.tld)
  在WEB-INF\tlds 下新建一个mytag.tld文件
自定义标签之HelloWorld?xml version="1.0" encoding="GBK" ?>
自定义标签之HelloWorld
自定义标签之HelloWorld
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
自定义标签之HelloWorld    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
自定义标签之HelloWorld    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
自定义标签之HelloWorld    version
="2.0">
自定义标签之HelloWorld    
<description>A tag library exercising SimpleTag handlers.</description>
自定义标签之HelloWorld    
<tlib-version>1.0</tlib-version>
自定义标签之HelloWorld
自定义标签之HelloWorld  
<short-name>examples</short-name>
自定义标签之HelloWorld  
<uri>/demotag</uri>
自定义标签之HelloWorld  
<description>
自定义标签之HelloWorld    A simple tab library for the examples
自定义标签之HelloWorld  
</description>
自定义标签之HelloWorld
自定义标签之HelloWorld  
自定义标签之HelloWorld
自定义标签之HelloWorld 
<tag>
自定义标签之HelloWorld    
<description>Outputs Hello, World,从实现Tag接口起开发</description>
自定义标签之HelloWorld        
<name>hello_int</name>
自定义标签之HelloWorld    
<tag-class>eflylab.HelloTag_Interface</tag-class>
自定义标签之HelloWorld    
<body-content>empty</body-content>
自定义标签之HelloWorld    
</tag>
自定义标签之HelloWorld  
自定义标签之HelloWorld
</taglib>
3:在web.xml中配置自定义标签
自定义标签之HelloWorld<?xml version="1.0" encoding="UTF-8"?>
自定义标签之HelloWorld
<web-app version="2.4" 
自定义标签之HelloWorld    xmlns
="http://java.sun.com/xml/ns/j2ee" 
自定义标签之HelloWorld    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
自定义标签之HelloWorld    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
自定义标签之HelloWorld     web-app_2_4.xsd"
>
自定义标签之HelloWorld  
<!-- 要正确在MyEclipse生成的web.xml中使用 tag 元素,必须将上面一行
自定义标签之HelloWorld      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd 改为
自定义标签之HelloWorld       web-app_2_4.xsd
-->
自定义标签之HelloWorld  
<taglib>
自定义标签之HelloWorld    
<taglib-uri>/demotag</taglib-uri>
自定义标签之HelloWorld    
<taglib-location>/WEB-INF/tlds/mytag.tld</taglib-location>
自定义标签之HelloWorld  
</taglib>
自定义标签之HelloWorld
</web-app>
4:使用的JSP页面
>

运行:自定义标签之HelloWorld
以实现Tag接口开发,我们看到我们的实现类中实现了Tag接口中的所有方法,我们比较关心的是doStartTag和doEndTag方法。

二:从TagSupport继承
上面的是从Tag接口开始编写标签库,现在还是以HelloWorld为例,看一下如何从TagSupport类继承
自定义标签之HelloWorldpackage eflylab;
自定义标签之HelloWorld
自定义标签之HelloWorld
import javax.servlet.jsp.*;
自定义标签之HelloWorld
import javax.servlet.jsp.tagext.*;
自定义标签之HelloWorld
import java.util.Hashtable;
自定义标签之HelloWorld
import java.io.Writer;
自定义标签之HelloWorld
import java.io.IOException;
自定义标签之HelloWorld
import java.util.Date;       
自定义标签之HelloWorld
 

在上面代码中,由于从TagSupport继承,因为前面说过,TagSupport是Tag接口的子接口ItrationTag接口的实现类,所以我们可以只覆盖两个方法就开发出了标签 ,这种比较简单,
由于可以在一个TLD文件中定义多个标签,所以我们还是用上面的mytag.tld文件定义这个标签
自定义标签之HelloWorld<?xml version="1.0" encoding="GBK" ?>
自定义标签之HelloWorld
自定义标签之HelloWorld
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
自定义标签之HelloWorld    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
自定义标签之HelloWorld    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
自定义标签之HelloWorld    version
="2.0">
自定义标签之HelloWorld    
<description>A tag library exercising SimpleTag handlers.</description>
自定义标签之HelloWorld    
<tlib-version>1.0</tlib-version>
自定义标签之HelloWorld      
<short-name>examples</short-name>
自定义标签之HelloWorld      
<uri>/demotag</uri>
自定义标签之HelloWorld      
<description>
自定义标签之HelloWorld        A simple tab library for the examples
自定义标签之HelloWorld      
</description>
自定义标签之HelloWorld
自定义标签之HelloWorld  
自定义标签之HelloWorld
自定义标签之HelloWorld 
<tag>
自定义标签之HelloWorld    
<description>Outputs Hello, World,从实现Tag接口起开发</description>
自定义标签之HelloWorld        
<name>hello_int</name>
自定义标签之HelloWorld    
<tag-class>eflylab.HelloTag_Interface</tag-class>
自定义标签之HelloWorld    
<body-content>empty</body-content>
自定义标签之HelloWorld
</tag>
自定义标签之HelloWorld    
自定义标签之HelloWorld  
<tag>
自定义标签之HelloWorld    
<name>hello</name>
自定义标签之HelloWorld    
<tag-class>eflylab.HelloTag</tag-class>
自定义标签之HelloWorld    
<body-content>empty</body-content>
自定义标签之HelloWorld    
<description>
自定义标签之HelloWorld    Simple hello world examples.
自定义标签之HelloWorld    Takes no attribute,and simply generates HTML
自定义标签之HelloWorld    
</description>
自定义标签之HelloWorld  
</tag>
自定义标签之HelloWorld
</taglib>
自定义标签之HelloWorld
测试页面
运行自定义标签之HelloWorld

相关文章:

猜你喜欢
  • 2022-02-09
  • 2021-11-05
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
  • 2021-06-30
相关资源
相似解决方案