什么也不说,直接上代码:
import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.IterationTag; import javax.servlet.jsp.tagext.Tag; import javax.servlet.jsp.tagext.TagSupport; //控制标签体重复执行 public class TagDemo3 extends TagSupport { int x = 5; @Override public int doStartTag() throws JspException { return Tag.EVAL_BODY_INCLUDE; } @Override public int doAfterBody() throws JspException { x--; if(x>0){ return IterationTag.EVAL_BODY_AGAIN; }else{ return IterationTag.SKIP_BODY; } } }
<%@taglib uri="/itcast" prefix="itcast" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>控制页面内容重复执行5次</title> </head> <body> <itcast:demo3> xxxxx </itcast:demo3> </body> </html>
<tag> <name>demo3</name> <tag-class>cn.itcast.web.tag.TagDemo3</tag-class> <body-content>JSP</body-content> </tag>