doStartTag():
此方法在标签开始的时候执行,此方法有两种返回值;分别是:
SKIP_BODY:表示忽略标签体的内容,而将执行权转交给doEndTag()方法;
EVAL_BODY_INCLUDE:表示执行标签体的内容;执行doAfterBody() ;
doAfterBody():
此方法是IterationTag接口与Tag接口的差别所在,本方法用来重复执行标签体的内容,有两种返回值,
分别是:
SKIP_BODY:表示标签体内容会被忽略,并将执行权转交个doEndTag()方法;
EVAL_BODY_AGAIN:表示重复执行标签体中的内容,会重复调用doAfterBody()方法,一直
循环执行下去,知道doAfterBody()方法返回SKIP_BODY为止;
doEndTag():
在标签结束时执行,此方法有两种返回值:
SKIP_PAGE:表示JSP页面应该立刻停止执行,并将所有的输出立刻回传到浏览器上;
EVAL_PAGE:表示JSP可以正常的运行完毕;
release():
释放所有的资源;
首先标签定义完成之后将执行的doStartTag()方法,之后判断此方法的返回值,如果是EVAL_BODY_INCLUDE
则表示要执行标签体(doAfterBody()),如果返回的是SKIP_BODY,则去执行doEndTag();
下面定义一个有标签体的标签;
本程序的主要功能是完成一个判断的操作,判断在一个属性范围之中,是否存在指定的属性名称,
如果存在则进行输出;
这个过程肯定是要使用标签体的;
package tag.lid.attribute;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class AttributeTag extends TagSupport{
private String name;//接受属性的名称
private String scope;//接受属性的范围
public int doStartTag() throws JspException{
Object value=null;
if("page".equals(this.scope)){//是否是page范围
value=super.pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE);
}
if("request".equals(this.scope)){//是否是request范围
value=super.pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE);
}
if("session".equals(this.scope)){//是否是session范围
value=super.pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE);
}
if("application".equals(this.scope)){//是否是application范围
value=super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE);
}
if(value==null){//表示现在根本就没有此属性
return TagSupport.SKIP_BODY;//没有属性不执行标签体
}
else{
return TagSupport.EVAL_BODY_INCLUDE;//执行标签体;
}
}
public void setName(String name){
this.name=name;
}
public void setScope(String scope){
this.scope=scope;
}
public String getName(){
return this.name;
}
public String getScope(){
return this.scope;
}
}
编写完成之后下面依然需要定义一个*.tld的文件,用于标签库的描述操作;
lidtag.tld文件:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>firsttag</short-name>
<tag>
<name>present</name>
<tag-class>
tag.lid.demo.AttributeTag
</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<attribute>
<attribute>
<name>scope</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<attribute>
</tag>
</taglib>
在web.xml中:
<taglib>
<taglib-uri>lid</taglib-uri>
<taglib-location>/WEB-INF/lidtag.tld</taglib-location>
</taglib>
那么下面开始进行标签的使用操作。
<%@ page contentType="text/html" pageEncoding="gbk"%>
<%@ taglib prefix="mytag" uri="lid"%>
<html>
<head><title>这是测试</title></head>
<body>
<%
String scope="session";
session.setAttribute("username","lid");
%>
<mytag:present name="username" scope="<%=scope%>">
<h3><%=scope%>范围存在属性,内容是:"${sessionScope.username}"</h3>
</mytag:present>
<mytag:present name="allusers" scope="request">
<h3>request范围存在属性,内容是:"${requestScope.allusers}"</h3>
</mytag:present>
</body>
</html>