Assign unique ids to <div> inside <c:forEach>

jsf, jsf-2, jstl

Solution

Use the `c:forEach` attribute `varStatus` to define a variable that will contain the status of the loop. Then you can use it in your template like this:

<c:forEach var="p" items="#{statusBean.statusList}" varStatus="loop">
        <h:form>
            <div class="status_#{loop.count}">
            // Content
            </div>
        </h:form>
</c:forEach>

You can also use `#{loop.index}` if you want it initiated with 0.

Problem

I want to assign a unique id for the `<div>` enclosed in the `<c:forEach>`. Whenever the page is rendered all the `<div>`s generated by the `<c:forEach>` have the same id. Is there any way to assign unique id for all the divs generated by the `<c:forEach>`? I have tried using `<ui:repeat>` but I was having issues with it so I decided to stick with `<c:forEach>`. Facelet: ``` <c:forEach var="p" items="#{statusBean.statusList}"> <h:form> <div class="status"> // Content </div> </h:form> </c:forEach> ```

Original source