小编Jac*_*cob的帖子

自定义 JSF 组件渲染器:如何围绕另一个元素渲染 HTML

我有 2 个元素,香蕉和一个 outputText,其中香蕉是一个自定义 JSF 组件,在香蕉渲染器中,我想生成包含指定元素的 HTML。

html:

<h:outputText id="theApple" value="This is an Apple" />
.
.
.
<my:banana for="theApple" link="http://www.banana.com" />
Run Code Online (Sandbox Code Playgroud)

香蕉渲染器(将目标元素包含在锚链接中):

@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");
}
Run Code Online (Sandbox Code Playgroud)

我想要达到的目标:

<a href="http://www.banana.com">This is an Apple</a>
.
.
.
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我不想在香蕉组件上编码,而是在它使用“for”属性定位的元素上进行编码。我看到的最接近的例子是 …

jsf encoding hyperlink custom-renderer custom-component

6
推荐指数
1
解决办法
1433
查看次数