6

我有 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#

如果有人能指出我正确的方向,那将不胜感激。谢谢你。

4

2 回答 2

5

您可以在PreRenderViewEvent. 在那一刻,所有组件都保证存在于树中,并且通过在树中添加/移动/删除组件来保证操作组件树是安全的。您可以通过 找到树中的其他组件UIComponent#findComponent()UIComponent#getParent()您可以使用和遍历组件树UIComponent#getChildren()。返回一个可变列表,getChildren()保证在PreRenderViewEvent.

假设您想用超链接包装给定组件,并且已经存在 JSF 超链接组件,<h:outputLink>那么扩展和重用它更容易,以保持代码简单和干燥。这是一个基本的启动示例:

@FacesComponent(createTag=true)
public class Banana extends HtmlOutputLink implements SystemEventListener {

    public Banana() {
        getFacesContext().getViewRoot().subscribeToViewEvent(PreRenderViewEvent.class, this);
    }

    @Override
    public boolean isListenerForSource(Object source) {
        return source instanceof UIViewRoot;
    }

    @Override
    public void processEvent(SystemEvent event) throws AbortProcessingException {
        String forClientId = (String) getAttributes().get("for");

        if (forClientId != null) {
            UIComponent forComponent = findComponent(forClientId);
            List<UIComponent> forComponentSiblings = forComponent.getParent().getChildren();
            int originalPosition = forComponentSiblings.indexOf(forComponent);
            forComponentSiblings.add(originalPosition, this);
            getChildren().add(forComponent);
        }
    }

}

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:my="http://xmlns.jcp.org/jsf/component"
>
    <h:head>
        <title>SO question 38197494</title>
    </h:head>
    <h:body>
        <h:outputText id="theApple" value="This is an Apple" />
        <my:banana for="theApple" value="http://www.banana.com" />
    </h:body>
</html>

请注意,children.remove()当您已经执行children.add(). 它会自动照顾孩子的父母。

于 2016-07-05T08:42:15.277 回答
0

如果我正确理解了这个问题,您想定义 2 个单独的 JSF 组件来生成通用 HTML 代码。一种可能的解决方案(如@BalusC 所示)是组件树操作,但这与在 xhtml 中以更自然的方式定义几乎相同:

<my:banana link="http://www.banana.com">
    <h:outputText value="This is an Apple" />
</my:banana>

如果出于某些需要,您想(重新)渲染通过for-attribute 引用的组件,我建议在客户端使用 javascript 进行 DOM 操作。您只需要使用适当的 javascript 代码script在渲染器中定义标签:banana

@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement("script", null);
    writer.writeAttribute("type", "text/javascript", null);
    // find DOM-element by 'for'-attribute, and wrap it with a hyperlink 
    writer.endElement("script");
}

这几乎就是 Primefaces 工具提示的构建方式。

好处是banana渲染器不知道如何<h:outputText>(或任何其他)渲染。

于 2016-07-05T09:48:05.403 回答