自定义标签【包含标签体】
实际意义: 就是在页面中从3开始,循环输出 时间
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;
}
}
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;
}
}
tld
<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>
<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>
注意JSP 的大写。 书上写的是小写,不对。
<myTag:bodyTag countNum=
"6">
<%= new java.util.Date() %>
</myTag:bodyTag>
<%= new java.util.Date() %>
</myTag:bodyTag>
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/282615,如需转载请自行联系原作者