本文共 1824 字,大约阅读时间需要 6 分钟。
package com.randy.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class BodyTag
extends BodyTagSupport {
private int countNum =0;
//循环显示时间的次数 private int currentNum =1;
//当前执行次数 @Override
public int doAfterBody()
throws JspException {
try {
JspWriter out = pageContext.getOut();
out.println(
"第"+currentNum+
"次执行标签体。标签体执行完毕。<br/>");
if(countNum >1){
countNum--;
currentNum ++;
return EVAL_BODY_TAG;
}
else{
return SKIP_BODY;
}
}
catch (IOException e) {
e.printStackTrace();
return SKIP_BODY;
}
}
@Override
public int doEndTag()
throws JspException {
try {
JspWriter out = pageContext.getOut();
bodyContent.writeOut(bodyContent.getEnclosingWriter());
//输出标签体的内容 out.println(
"标签结束了");
}
catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
@Override
public void doInitBody()
throws JspException {
currentNum=3;
super.doInitBody();
}
@Override
public int doStartTag()
throws JspException {
try {
JspWriter out = pageContext.getOut();
out.println(
"标签开始了:<br/>");
if(countNum>0){
return EVAL_BODY_TAG;
}
else{
return SKIP_BODY;
}
}
catch (IOException e) {
e.printStackTrace();
return SKIP_BODY;
}
}
public int getCountNum() {
return countNum;
}
public void setCountNum(
int countNum) {
this.countNum = countNum;
this.currentNum =1;
}
}
<tag>
<description>含有标签体</description>
<name>bodyTag</name>
<tag-
class>com.randy.tag.BodyTag</tag-
class>
<body-content>JSP</body-content>
<attribute>
<name>countNum</name>
<required>
true</required>
<rtexprvalue>
true</rtexprvalue>
</attribute>
</tag>
<myTag:bodyTag countNum=
"6">
<%=
new java.util.Date() %>
</myTag:bodyTag>
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/282615,如需转载请自行联系原作者