我的海关标签如下。 repeat和heading标签实现了doAfterBody
方法。它们都扩展了BodyTagSupport类
<csajsp:repeat reps="5">
<LI>
<csajsp:heading bgColor="BLACK">
White on Black Heading
</csajsp:heading>
</LI>
</csajsp:repeat>
重复标记类
public void setReps(String repeats) {
System.out.println("TESTING"+repeats);
//sets the reps variable.
}
public int doAfterBody() {
System.out.println("Inside repeate tag"+reps);
if (reps-- >= 1) {
BodyContent body = getBodyContent();
try {
JspWriter out = body.getEnclosingWriter();
System.out.println("BODY"+body.getString());
out.println(body.getString());
body.clearBody(); // Clear for next evaluation
} catch(IOException ioe) {
System.out.println("Error in RepeatTag: " + ioe);
}
return(EVAL_BODY_TAG);
} else {
return(SKIP_BODY);
}
}
标题类标记
public int doAfterBody()
{
System.out.println("inside heading tag");
BodyContent body = getBodyContent();
System.out.println(body.getString());
try {
JspWriter out = body.getEnclosingWriter();
out.print("NEW TEXT");
} catch(IOException ioe) {
System.out.println("Error in FilterTag: " + ioe);
}
// SKIP_BODY means I'm done. If I wanted to evaluate
// and handle the body again, I'd return EVAL_BODY_TAG.
return(SKIP_BODY);
}
public int doEndTag() {
try {
JspWriter out = pageContext.getOut();
out.print("NEW TEXT 2");
} catch(IOException ioe) {
System.out.println("Error in HeadingTag: " + ioe);
}
return(EVAL_PAGE); // Continue with rest of JSP page
}
自定义标记tld文件是
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>csajsp</shortname>
<uri></uri>
<tag>
<name>heading</name>
<tagclass>com.test.tags.HeadingTag</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>bgColor</name>
<required>true</required> <!-- bgColor is required -->
</attribute>
</tag>
<tag>
<name>repeat</name>
<tagclass>com.test.tags.RepeatTag</tagclass>
<info>Repeats body the specified number of times.</info>
<bodycontent>JSP</bodycontent>
<attribute>
<name>reps</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
打印SOP的顺序是
我不知道为什么它没有调用doAfterBody
标记的csajsp:repeat
。
请帮助我理解这一点。
答案 0 :(得分:0)
您的代码会扩展哪个类?
TagSupport的默认行为是返回SKIP_BODY
,这将跳过标记正文的处理。
BodyTagSupport的默认行为是返回EVAL_BODY_BUFFERED
,它将处理标记正文。
如果您自己实现BodyTag,那么您需要确保正确覆盖doStartTag以指示应该评估JSP主体。