欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TagSupport

程序员文章站 2022-06-08 18:07:30
...

定义最简单的标签
自定义标签采用Default Adapter模式(缺省适配模式)
Java代码
//最简单的标签
public class LangHuaTag extends TagSupport {
private long startTime;
private long endTime;

public int doStartTag() throws JspException {
startTime = System.currentTimeMillis();
//表示定制标记里面有所包括的JSP页面
return TagSupport.EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
endTime = System.currentTimeMillis();
long elapsed = endTime - startTime;
try {
JspWriter out = pageContext.getOut();
out.println("runtime is "+ elapsed);
} catch (IOException e) {
e.printStackTrace();
}
//表示JSP页面继续运行
return TagSupport.EVAL_PAGE;
}

}
//代属性的标签
public class DateTag extends TagSupport {
private String pattern = "yyyy-MM-dd hh:mm:ss";
private Date date;
//必须要有Set方法,因为是属性可以设值
public void setPattern(String pattern) {
this.pattern = pattern;
}

public void setDate(Date date) {
this.date = date;
}

public int doEndTag() throws JspException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
//如果没有就是当前时间
if(date==null){
date = new Date();
}
JspWriter out = pageContext.getOut();
try {
out.print(sdf.format(date));
} catch (IOException e) {
e.printStackTrace();
}
return TagSupport.EVAL_PAGE;
}
}

/**
* 循环输出
* @author Administrator
*
*/
public class LoopTag extends TagSupport {
private int times =0;
//Set方法设值
public void setTimes(int times) {
this.times = times;
}

public int doStartTag() throws JspException {
//表示定制标记里面有所包括的JSP页面
return TagSupport.EVAL_BODY_INCLUDE;
}

public int doAfterBody() throws JspException {
if(times>0){
times--;
//表示双从标签开始输入
return TagSupport.EVAL_BODY_AGAIN;
}
//表示结束,忽略标签内部的内容
return TagSupport.SKIP_BODY;
}

}

//最简单的标签
public class LangHuaTag extends TagSupport {
private long startTime;
private long endTime;

public int doStartTag() throws JspException {
startTime = System.currentTimeMillis();
//表示定制标记里面有所包括的JSP页面
return TagSupport.EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
endTime = System.currentTimeMillis();
long elapsed = endTime - startTime;
try {
JspWriter out = pageContext.getOut();
out.println("runtime is "+ elapsed);
} catch (IOException e) {
e.printStackTrace();
}
//表示JSP页面继续运行
return TagSupport.EVAL_PAGE;
}

}
//代属性的标签
public class DateTag extends TagSupport {
private String pattern = "yyyy-MM-dd hh:mm:ss";
private Date date;
//必须要有Set方法,因为是属性可以设值
public void setPattern(String pattern) {
this.pattern = pattern;
}

public void setDate(Date date) {
this.date = date;
}

public int doEndTag() throws JspException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
//如果没有就是当前时间
if(date==null){
date = new Date();
}
JspWriter out = pageContext.getOut();
try {
out.print(sdf.format(date));
} catch (IOException e) {
e.printStackTrace();
}
return TagSupport.EVAL_PAGE;
}
}

/**
* 循环输出
* @author Administrator
*
*/
public class LoopTag extends TagSupport {
private int times =0;
//Set方法设值
public void setTimes(int times) {
this.times = times;
}

public int doStartTag() throws JspException {
//表示定制标记里面有所包括的JSP页面
return TagSupport.EVAL_BODY_INCLUDE;
}

public int doAfterBody() throws JspException {
if(times>0){
times--;
//表示双从标签开始输入
return TagSupport.EVAL_BODY_AGAIN;
}
//表示结束,忽略标签内部的内容
return TagSupport.SKIP_BODY;
}

}


配置文件
Xml代码
<?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_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>util</short-name>
<uri>http://langhua.com/taglib/util</uri>
<tag>
<name>timer</name>
<tag-class>com.langhua.tagsupport.LangHuaTag</tag-class>
<body-content>JSP</body-content>
<!-- JSP,empty表示能能包函内容的,scriptless,tagdependent -->
</tag>

<tag>
<name>date</name>
<tag-class>com.langhua.tagsupport.DateTag</tag-class>
<body-content>empty</body-content>
<!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->
<attribute>
<!-- 标签名 -->
<name>time</name>
<!-- 是否为可选属性 -->
<required>false</required>
<!-- 是否接受JSP表达示计算结果 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pattern</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>

<tag>
<name>loop</name>
<tag-class>com.langhua.tagsupport.LoopTag</tag-class>
<body-content>JSP</body-content>
<!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->
<attribute>
<!-- 标签名 -->
<name>times</name>
<!-- 是否为可选属性 -->
<required>true</required>
<!-- 是否接受JSP表达示计算结果 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

<?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_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>util</short-name>
<uri>http://langhua.com/taglib/util</uri>
<tag>
<name>timer</name>
<tag-class>com.langhua.tagsupport.LangHuaTag</tag-class>
<body-content>JSP</body-content>
<!-- JSP,empty表示能能包函内容的,scriptless,tagdependent -->
</tag>

<tag>
<name>date</name>
<tag-class>com.langhua.tagsupport.DateTag</tag-class>
<body-content>empty</body-content>
<!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->
<attribute>
<!-- 标签名 -->
<name>time</name>
<!-- 是否为可选属性 -->
<required>false</required>
<!-- 是否接受JSP表达示计算结果 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pattern</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>

<tag>
<name>loop</name>
<tag-class>com.langhua.tagsupport.LoopTag</tag-class>
<body-content>JSP</body-content>
<!-- JSP,empty表示不能包函内容的,scriptless,tagdependent -->
<attribute>
<!-- 标签名 -->
<name>times</name>
<!-- 是否为可选属性 -->
<required>true</required>
<!-- 是否接受JSP表达示计算结果 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>


JSP页面
Html代码
<%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%>

<util:timer></util:timer>
<util:loop times="3">
<util:date pattern="yyyy-MM-dd" /><br/>
</util:loop>

<%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%>

<util:timer></util:timer>
<util:loop times="3">
<util:date pattern="yyyy-MM-dd" /><br/>
</util:loop>

TagSupport的流程图

EVAL_BODY_INCLUDE 处理标记内容,并把这些内容写到输出流中doStartTag()
SKIP_BODY 不处理标记内容doStartTag(),doAfterBody()
EVAL_BODY_AGAIN 又重头处理doAfterBody()
EVAL_PAGE 继续执行JSP里面的代码 doEndTag()
SKIP_PAGE 不继续执行JSP里面的代码 doEndTag()

[img]http://langhua9527.iteye.com/upload/attachment/77593/2ce5c561-a5ea-36d1-9082-25e62273dcb2.jpg[/img]