问题
I want to create a composite component where to acutal layout of of iterating elements can be passed to the composite. This is a simplified example and works:
<composite:interface>
<composite:attribute name="value"/>
</composite:interface>
<composite:implementation>
<ul>
<c:forEach var="i" items="#{cc.attrs.value}">
<li>
<h:outputText value="Test #{i.name}"/>
</li>
</c:forEach>
</ul>
But I don't want the h:outputText
to be hardcoded in the component. When using the component I'm trying to have something like this:
<my:list var="user" value="#{myBean.userList}">
<h:outputText value="Test #{user.name}"/>
</my:list>
Is assume that I have to use a var
, but I don't know how to handle this in my component and access the child <h:outputText value="Test #{user.name}"/>
correctly.
回答1:
You could use <composite:insertChildren />
to be able to "pass" the child components defined to your composite component definition. Also I recommend using <ui:repeat>
instead of <c:forEach>
because it's a real iterative component and better suited for JSF. Here is an example how to implement your component:
<composite:interface>
<composite:attribute name="value"/>
</composite:interface>
<composite:implementation>
<ul>
<ui:repeat var="item" value="#{cc.attrs.value}">
<li>
<composite:insertChildren />
</li>
</ui:repeat>
</ul>
</composite:implementation>
Usage:
<my:list value="#{myBean.userList}">
<h:outputText value="Test #{item.name}"/>
</my:list>
来源:https://stackoverflow.com/questions/13356967/jsf-iterative-composite-component-with-customizable-content