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;
}
}
<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>
<%= new java.util.Date() %>
</myTag:bodyTag>

















