Home > OS >  How to iterate through only ONE value in JSTL?
How to iterate through only ONE value in JSTL?

Time:12-14

<c:forEach varStatus="status" var="friends" items="${profile.friends}">
    <c:if test="${friends.selectedCount != -1}">
        <th id="closeFriends" style="text-align:center;"></th>
    </c:if>
</c:forEach>

I don't want to loop through all friends, I just want to loop through ONE friend and check their selected count and display my th off of that. How do I do that?

CodePudding user response:

The question is badly formulated but the concrete functional requirement is understood. You basically want the EL equivalent of the following plain Java equivalent:

if (profile.getFriends().stream().anyMatch(f -> f.getSelectedCount() != -1)) {
    // ...
}

This very functionality is also readily available in EL since EL version 3.0 (Java EE 7 / Servlet 3.1). So this should do it for you:

<c:if test="${profile.friends.stream().anyMatch(f -> f.selectedCount != -1).orElse(false)}">
    <th id="closeFriends" style="text-align:center;"></th>
</c:if>
  • Related