如何通过其接口定义复合内部中继器的内容?

How to define the content of repeater inside composite through its interface?

我有一个带有 ui:repeat 的复合组件,想通过复合接口定义 ui:repeat 的内容。

以下代码在 MyFaces 中有效,但看起来更像是一个 hack,因为变量名称 varRepeat 必须在复合之外已知,并且只有在没有提供其他应在其他地方呈现的子项时才有效。

查看

定义ui:repeat

的内容
<comp:myRepeater value="#{of:createIntegerArray(1,5)}">
    <h:outputText id="varComp" value="#{varRepeat}"/>
</comp:myRepeater>

复合 myRepeater

<composite:attribute name="value" type="java.lang.Object"/>
<composite:implementation>
    <ui:repeat var="varRepeat" value="#{cc.attrs.value}">
        <composite:insertChildren/>
    </ui:repeat>
</composite:implementation>

考虑到 var 属性不支持 EL,这已经是最好的了。为了让最终用户清楚,请在 <cc:interface shortDescription> and/or <cc:attribute shortDescription>.

中记录 var 的名称
<cc:interface>
    <cc:attribute name="value" type="java.util.Collection" 
        shortDescription="A collection of items. Each item is exposed in EL under the variable name 'item'." />
<cc:interface>
<cc:implementation>
    <ui:repeat value="#{cc.attrs.value}" var="item">
        <cc:insertChildren />
    </ui:repeat>
</cc:implementation>

用法:

<my:repeat value="#{bean.list}">
    <h:outputText value="#{item}" />
</my:repeat>

OmniFaces showcase application has also a similar composite for long: <os:listDocs>:

<cc:implementation>
    <c:set var="docs" value="#{page.current.documentation[cc.attrs.paths]}" />
    <ui:fragment rendered="#{not empty docs}">
        <h3>#{cc.attrs.header}</h3>
        <ul>
            <ui:repeat value="#{docs}" var="path">
                <li><a href="#{cc.attrs.url}"><code>#{cc.attrs.label}</code></a></li>
            </ui:repeat>
        </ul>
    </ui:fragment>
</cc:implementation>

Usage:

<os:listdocs header="VDL" paths="vdl" url="#{_vdlURL}#{path}.html" label="#{fn:replace(path,'/', ':')}" />
<os:listdocs header="API" paths="api" url="#{_apiURL}#{path}.html" label="#{fn:replace(path,'/', '.')}" /> 
<os:listdocs header="Source code" paths="api" url="#{_srcURL}#{path}.java" label="#{fn:replace(path,'/', '.')}" />

作为设计提示,如果您为集合使用合理的属性名称,var 可能会变得更加自我记录。例如。要求 items 作为值并提供 var="item".

<my:repeat items="#{[1,2,3,4,5]}">
    <h:outputText value="#{item}"/>
</my:repeat>