JSF Iterative composite component with customizable content
Asked Answered
B

1

3

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.

Bicollateral answered 13/11, 2012 at 7:30 Comment(0)
I
4

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>
Igbo answered 13/11, 2012 at 8:33 Comment(3)
Thank you, this works. But it's not so nice to have the var="item" hardcoded.Bicollateral
You could try with <ui:repeat var="#{cc.attrs.var}" /> but I'm not sure it is going to work.Igbo
However having the var name hardcoded is usable and not that intrusive.Igbo

© 2022 - 2024 — McMap. All rights reserved.