我有 2 个元素,banana 和 outputText,其中banana 是一个自定义 JSF 组件,在banana 渲染器中,我想生成包含指定元素的 HTML。
xhtml:
<h:outputText id="theApple" value="This is an Apple" />
.
.
.
<my:banana for="theApple" link="http://www.banana.com" />
香蕉渲染器(将目标元素包含在锚链接中):
@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
ResponseWriter responseWriter = context.getResponseWriter();
Banana banana = Banana.class.cast(component);
responseWriter.startElement("a", null);
responseWriter.writeAttribute("id", banana.getClientId(context), null);
responseWriter.writeAttribute("href", banana.getLink(), null);
}
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
ResponseWriter responseWriter = context.getResponseWriter();
Banana banana = Banana.class.cast(component);
responseWriter.endElement("a");
}
我想要达到的目标:
<a href="http://www.banana.com">This is an Apple</a>
.
.
.
如您所见,我不想在香蕉组件上编码,而是在使用“for”属性的目标元素上进行编码。我看到的最接近的例子是 PrimeFaces 工具提示,但我不太明白它是如何利用“for”属性的。 http://www.primefaces.org/showcase/ui/overlay/tooltip/tooltipOptions.xhtml#
如果有人能指出我正确的方向,那将不胜感激。谢谢你。