2012-06-17 38 views
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:dataTableui:repeat)。

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

回答

2

這不會裏面<ui:repeat><cc:insertChildren>工作作爲<cc:insertChildren>在視圖生成時進行評估,而不是在視圖渲染時間。 #{item}僅在查看渲染時間期間可用,因爲<ui:repeat>僅在查看渲染時間期間運行。

當您使用JSTL <c:forEach>代替它時,它將起作用,因爲它在視圖編譯期間也會被評估,如<cc:insertChildren>本身。

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

當您在另一個重複的JSF組件中輪流使用組合時,請小心,否則它將無法工作。另見JSTL in JSF2 Facelets... makes sense?

一種替代,除了創建延伸UIDataUIRepeat的定製組件,是使用<ui:decorate>代替。

首先要customList.xhtml正常的模板:

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

然後你就可以按如下方式使用它:

<ui:decorate template="customList.xhtml"> 
    <ui:param name="list" value="#{bean.list}" /> 
    <ui:define name="item"> 
     #{item} 
    </ui:define> 
</ui:decorate> 
+0

只是使用'ui:decorate'的模板不是我想要的,因爲我試圖添加更多的功能,如延遲加載等(爲簡單起見,此處省略)。關於'c:forEach'我該如何公開在CC中定義的#{item}以在我使用CC的頁面上使用? –

+0

和你的問題一樣。你所需要的只是用''代替''。 – BalusC

+0

實際上'#{listRetriever.list}'是使用 '構建的,我想這會發生在'c: forEach'循環已完成迭代,所以這就是爲什麼它不適用於我.. –

1

這些天,我一直試圖做同樣的事情,沒有得到解決。

我的解決方案,我自己的組件上的var屬性的值永遠不能配置它。所以保持固定的名稱「var」,並始終暴露給我的組件,我使用變量「var」。的MI代碼

實施例:

組件: 文件: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>