【发布时间】:2011-11-01 19:15:04
【问题描述】:
我想编写一个自定义 JSP 标记,其输出包括其他 JSP 标记,这些标记本身也应该被动态评估。但显然我的TagSupport 子类写给pageContext.getOut() 的所有内容都直接发送给客户端,无需进一步评估。
我觉得这应该很简单,因为这似乎是人们想要使用自定义标签的第一件事:封装和重用其他自定义标签,避免代码重复。
如何让下面的代码做它明显想做的事情?:
public class MyTag extends TagSupport {
public int doStartTag() throws JspException {
try {
pageContext.getOut().println(
"The output from this tag includes other tags " +
"like <mypackage:myOtherTag>this one</mypackage:myOtherTag> " +
"which should themselves be evaluated and rendered."
)
} catch (IOException e) {
throw new JspException(e);
}
return SKIP_BODY;
}
}
编辑:我的特定用例的一些背景,如果有帮助的话。我有一个自定义标签<user>,它以对我的应用程序有用的方式动态呈现用户名(鼠标悬停在名字、姓氏、电话号码等)。我现在正在编写另一个标签<comment> 来显示用户cmets,我想使用我现有的<user> 标签在<comment> 标签的输出中呈现用户名。
【问题讨论】: