1

我正在迭代复合组件中的项目列表。我想公开列表中的每个项目,以便它们可以在此复合组件的子组件中使用,以创建一个模板来显示列表中的所有项目。

这是我的复合组件实现:

customList.xhtml

<ui:component
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:cc="http://java.sun.com/jsf/composite"
    xmlns:ui="http://java.sun.com/jsf/facelets">    

    <cc:interface>
    </cc:interface>

    <cc:implementation>       
        ...
        ...   
        <ui:repeat value="#{listRetriever.list}" var="item">
            <cc:insertChildren />
        </ui:repeat>

    </cc:implementation>
</ui:component>

现在我想#{item}在我的页面中定义复合组件的子组件时使用(类似于h:dataTableor ui:repeat)。

  <my:customList>
        #{item}                <!--- over here--->
  </my:customList>
4

2 回答 2

2

That won't work with <cc:insertChildren> inside <ui:repeat> as the <cc:insertChildren> is evaluated during view build time, not during view render time. The #{item} is only available during view render time as the <ui:repeat> runs during view render time only.

It'll work when you use JSTL <c:forEach> instead as it's also evaluated during view build time like the <cc:insertChildren> itself.

xmlns:c="http://java.sun.com/jsp/jstl/core"
...
<c:forEach items="#{listRetriever.list}" var="item">
    <cc:insertChildren />
</c:forEach>

Be careful when you use the composite in turn inside another repeating JSF component, it wouldn't work then. See also JSTL in JSF2 Facelets... makes sense?.

An alternative, apart from creating a custom component which extends UIData or UIRepeat, is using <ui:decorate> instead.

First make customList.xhtml a normal template:

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    ... 
    ...  
    <ui:repeat value="#{list}" var="item">
        <ui:insert name="item" />
    </ui:repeat>
    ...
    ...
</ui:composition>

Then you can use it as follows:

<ui:decorate template="customList.xhtml">
    <ui:param name="list" value="#{bean.list}" />
    <ui:define name="item">
        #{item}
    </ui:define>
</ui:decorate>
于 2012-06-17T14:24:34.717 回答
1

这些天我试图做类似的事情,没有解决。

我的解决方案,我自己的组件上的 var 属性的值永远无法配置。所以保持固定在名称“var”下,并始终暴露给我的组件,我使用变量“var”。

小米代码示例:

组件: 文件:listadoPaginado.xhtml

<composite:interface>
    <composite:attribute name="id" required="true" 
                         displayName="ID if component" 
                         shortDescription="ID of component" />
    <composite:attribute name="value" required="true" 
                         displayName="List with values to iterate"/>  
</composite:interface>

<!-- Implementacion de la estructura del componente -->
<composite:implementation>
    <h:panelGroup id="#{cc.attrs.id}_panel_comp">
        <br/>
        <div  style="#{cc.attrs.style}">

            <ui:repeat  var="var" value="#{cc.attrs.value.model}">
                <composite:insertChildren/>
            </ui:repeat>
        </div>
    </h:panelGroup>
</composite:implementation>

组件的使用:

<gb:listadoPaginado id="listado_garantias"
                            value="#{beanTest.myList}">
 <p:panel>
     <p:panelGrid>
         <p:row>
              <p:column>
                   <h:outputLabel value="#{var.description}"/>
               </p:column>
               <p:column>
                    <p:inputText value="#{var.longDescription}"/>
                </p:column>
          </p:row>
       </p:panelGrid>
   </p:panel>

于 2012-06-19T19:58:38.767 回答