📅  最后修改于: 2021-01-05 01:13:41             🧑  作者: Mango
我们可以使用IterationTag接口的doAfterBody()方法来迭代任何标签的主体内容。
在这里,我们将使用实现IterationTag接口的TagSupport类。为了迭代主体内容,我们需要在doAfterBody()方法中使用EVAL_BODY_AGAIN常量。
在此示例中,我们将在自定义标签中使用属性,该属性返回任何给定数字的幂。我们在这里创建了三个文件
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>
3 ^ 5 =
body
package com.javatpoint.taghandler;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class PowerNumber extends TagSupport{
private int number;
private int power;
private static int counter;
private static int result=1;
public void setPower(int power) {
this.power = power;
}
public void setNumber(int number) {
this.number = number;
}
public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() {
counter++;
result *= number;
if (counter==power)
return SKIP_BODY;
else
return EVAL_BODY_AGAIN;
}
public int doEndTag() throws JspException {
JspWriter out=pageContext.getOut();
try{
out.print(result);
}catch(Exception e){e.printStackTrace();}
return EVAL_PAGE;
}
}
1.0
1.2
simple
http://tomcat.apache.org/example-taglib
A simple tab library for the examples
power
com.javatpoint.taghandler.PowerNumber
number
true
power
true
让我们创建一个循环标签,该标签迭代该标签的主体内容。
Insert title here
<%@taglib prefix="m" uri="sssuri" %>
My Name is khan
1.0
1.2
abc
sssuri
loop
com.javatpoint.customtag.Loop
start
true
end
true
package com.javatpoint.customtag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class Loop extends TagSupport{
private int start=0;
private int end=0;
public void setStart(int start) {
this.start = start;
}
public void setEnd(int end) {
this.end = end;
}
@Override
public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
if(start
sssuri
/WEB-INF/mytags.tld