在复合组件内部引用组件(反之亦然)

Rob*_*och 3 jsf primefaces composite-component

我在使用 Primefaces (v5.2) 时遇到一两个问题:

复合组件中引用组件

假设我有一个包含输入字段的复合组件:

我的输入字段.xhtml

<composite:interface>
    <composite:attribute name="value" />
    ...
</composite:interface>

<composite:implementation>
    <h:inputText value="#{cc.attrs.value}" />
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

(当然,真正的应用程序做得“多一点”。)

在我的页面中,我现在像这样使用这个字段:

索引.xhtml

<my:myinputputfield value=#{controller.inputstring} />
Run Code Online (Sandbox Code Playgroud)

这有效。但是:知道我想从外部引用该内部输入字段,例如用于标签或消息。东西喜欢

<composite:interface>
    <composite:attribute name="value" />
    ...
</composite:interface>

<composite:implementation>
    <h:inputText value="#{cc.attrs.value}" />
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

当然这不起作用,因为id没有为myinputfield. 所以第一个想到的想法是像这样扩展 cc:

myinputfield.xhtml (新)

<composite:interface>
    <composite:attribute name="id" />
    <composite:attribute name="value" />
    ...
</composite:interface>

<composite:implementation>
    <h:inputText id="{cc.attrs.id}" value="#{cc.attrs.value}" />
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

这也不起作用。我尝试了不同的事情并阅读了不同的答案和文章,但没有找到答案。


第二个问题正好相反:

复合组件之外引用组件

这一次换个角度想象一下。我有一个自定义的标签、消息或在我的情况下是一个工具提示:

我的工具提示.xhtml

<composite:interface>
    <composite:attribute name="for" />
    <composite:attribute name="value" />
    ...
</composite:interface>

<composite:implementation>
    <p:toolTip for="#{cc.attrs.for}" value="#{cc.attrs.value}" />
</composite:implementation>
Run Code Online (Sandbox Code Playgroud)

这次我想附加mytooltip到现有组件:

索引.xhtml

<h:outputtext id="ot" value="Hello World!" />
<my:mytooltip for="ot" value="since 1974" />
Run Code Online (Sandbox Code Playgroud)

这也不起作用。(当然!?)

我前段时间遇到了这个问题,并通过在复合组件中包含 outputText 来解决它。


但我觉得应该可以管理这两个用户案例。但是如何?

Mic*_*tti 5

  1. 复合组件中引用组件

    给内部输入一个静态ID

    <composite:interface>
        <composite:attribute name="value" />
        ...
    </composite:interface>
    
    <composite:implementation>
        <h:inputText id="input" value="#{cc.attrs.value}" />
    </composite:implementation>
    
    Run Code Online (Sandbox Code Playgroud)

    与任何命名容器一样引用内部组件:

    <p:inputLabel for="mif:input" value="Your Input:"/>
    <my:myinputputfield id="mif" value=#{controller.inputstring} />
    <p:message for="mif:input" />
    
    Run Code Online (Sandbox Code Playgroud)


  1. 复合组件之外引用组件

    规范的方式是使用完整的客户端ID:

    <h:form id="form">
        <h:outputText id="ot" value="Hello World!" />
        <my:mytooltip for=":form:ot" value="since 1974" />
    </h:form>
    
    Run Code Online (Sandbox Code Playgroud)

    但是,由于您将搜索表达式传递给 PF 组件,您还可以:

    <h:form>
        <h:outputText id="ot" value="Hello World!" />
        <my:mytooltip for="@form:ot" value="since 1974" />
    </h:form>
    
    Run Code Online (Sandbox Code Playgroud)

    或一般:

    <p:tabView>
        <p:tab title="random tab">
            <h:outputText id="ot" value="Hello World!" />
            <my:mytooltip for="@namingcontainer:ot" value="since 1974" />
        </p:tab>
    </p:tabView>
    
    Run Code Online (Sandbox Code Playgroud)

    甚至:

    <h:outputText value="Hello World!" />
    <my:mytooltip for="@composite:@previous" value="since 1974" />
    
    Run Code Online (Sandbox Code Playgroud)

    然而,在这种情况下,标签组件/facelet 标签文件可能是更好的方法。