【问题标题】:Referencing components inside of composite components (and vice versa)引用复合组件内部的组件(反之亦然)
【发布时间】:2017-11-24 11:51:18
【问题描述】:

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

引用组件内部复合组件

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

myinputfield.xhtml

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

<composite:implementation>
    <h:inputText value="#{cc.attrs.value}" />
</composite:implementation>

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

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

index.xhtml

<my:myinputputfield value=#{controller.inputstring} />

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

<p:inputLabel for="mif" value="Your Input:"/>
<my:myinputputfield id="mif" value=#{controller.inputstring} />
<p:message for="mif" />

这当然行不通,因为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>

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


第二个问题完全相反:

引用组件外部复合组件

这次反过来想像。我有一个自定义标签、消息或工具提示:

mytooltip.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>

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

index.xhtml

<h:outputtext id="ot" value="Hello World!" />
<my:mytooltip for="ot" value="since 1974" />

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

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


但我觉得应该可以管理这两个用户案例。但是怎么做呢?

【问题讨论】:

  • 最好顺便说一句,每个问题问 1 个问题。这种情况下的问题是复合组件总是命名容器。阅读这些上下文中的内容(并检查 html 源代码以查看它的实际效果,检查输入文本的真实客户端 ID)

标签: jsf primefaces composite-component


【解决方案1】:
  1. 引用组件内部复合组件

    给内部输入一个静态id

    <composite:interface>
        <composite:attribute name="value" />
        ...
    </composite:interface>
    
    <composite:implementation>
        <h:inputText id="input" value="#{cc.attrs.value}" />
    </composite:implementation>
    

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

    <p:inputLabel for="mif:input" value="Your Input:"/>
    <my:myinputputfield id="mif" value=#{controller.inputstring} />
    <p:message for="mif:input" />
    


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

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

    <h:form id="form">
        <h:outputText id="ot" value="Hello World!" />
        <my:mytooltip for=":form:ot" value="since 1974" />
    </h:form>
    

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

    <h:form>
        <h:outputText id="ot" value="Hello World!" />
        <my:mytooltip for="@form:ot" value="since 1974" />
    </h:form>
    

    或一般来说:

    <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>
    

    甚至:

    <h:outputText value="Hello World!" />
    <my:mytooltip for="@composite:@previous" value="since 1974" />
    

    但是,在这种情况下,tag-component/facelet-tag-file 可能是更好的方法。

【讨论】:

  • add 1:引用有效,结果证明我过度简化了问题。 (这是关于一个有效的参数化“必需”标签,但不会在标签上生成星号。如果我自己无法解决这个问题,我会提出另一个问题。) - 添加 2:我我有空的时候会调查一下。无论如何,这可能是值得一读的。
猜你喜欢
  • 2021-04-22
  • 2019-07-31
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多