Search code examples
jsfjsf-2faceletscomposite-component

JSF Composite component iterating and displaying a list of objects


In JSP and JSTL I would normaly do something like this:

<c:forEach items="${userList}" var = "user">
    <div id = "user-block">
        <h1>${user.name}</h1>
        <div id = "user-description">
            <p>${user.description}</p>
        </div>
        <ul>
            <li> Age: ${user.age} </li>
            <li> City: ${user.city} </li>
            <li> Country: ${user.country} </li>
        </ul>
    </div>
</c:forEach>

I'm trying to gain the same result using Facelet Composite Components:

<cc:interface>
    <cc:attribute name="value" type="java.util.List" required="true" shortDescription="The list of objects that should be displayed"/>
</cc:interface>

<cc:implementation>
    <div class = "event-block">

    </div>
</cc:implementation>

The problem is that I don't know how to iterate over the objects in #{cc.attrs.value}.

LE: I would like to know if there is a way to solve this without using JSP or JSTL


Solution

  • Use ui:repeat instead of c:forEach.

    <ui:repeat value="#{cc.attrs.value}" var="user">
        <h1>#{user.name}</h1>
        ...
    </ui:repeat>
    

    See https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat for further comparison of c:forEach and ui:repeat.