Search code examples
spring-bootjspjstl

How do I write an if statement in JSP-JSTL if I need to check whether the user list contains a specific user?


'theGroup' and 'groupCreator' are a model(entity) attributes coming from the controller. 'users' is the array list which is the attribute of theGroup (theGroup.getUsers() basically).

I need to check if the groupCreator exists in theGroup.users list in JSP

I've tried the code below but it didn't work

<c:if test = "${theGroup.users.contains(groupCreator)}">

</c:if>

Solution

  • You can use forEach to iterate through values and then compare values in it with groupCreator. Your code will somewhat look like below :

         <c:forEach var="values" items="${theGroup.users}">
            <c:if test="${values == groupCreator}">
                <!--setting true if value match-->
                <c:set var="Matched" value="true" scope="request" />
            </c:if>
         </c:forEach
         <!--Print-->
            ${Matched}