自定义标签实现问题
问题描述:
我有海关标签如下。重复和标题标签有他们的doAfterBody
方法implemented.Both扩展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的打印顺序是csajsp的
- setter方法:重复被调用。
- 打印黑色标题上的白色。即csajsp的doAfterBody:标题标签被调用。
我不知道为什么它不叫csajsp:repeat
标签doAfterBody
。
请帮我理解这一点。
答
你的标签延伸到什么级别?
TagSupport的默认行为是返回SKIP_BODY
,这将跳过处理标签正文。
BodyTagSupport的默认行为是返回EVAL_BODY_BUFFERED
,这将处理标记正文。
如果您自己实施BodyTag,那么您需要确保正确覆盖doStartTag以指示应评估JSP正文。
没有足够的信息。我们需要关联TLD文件的一部分,并且我们需要知道您的标记正在扩展哪个类/接口 – skaffman 2010-03-31 08:59:23
嗨,我已经用足够的信息更新了线程。你可以帮我吗? – Appps 2010-03-31 12:36:12