Search code examples
javajspjstl

Evaluate list.contains string in JSTL


I need to hide an element if certain values are present in the JSP

The values are stored in a List so I tried:

<c:if test="${  mylist.contains( myValue ) }">style='display:none;'</c:if>

But, it doesn't work.

How can I evaluate if a list contains a value in JSTL, the list and the values are strings.


Solution

  • Sadly, I think that JSTL doesn't support anything but an iteration through all elements to figure this out. In the past, I've used the forEach method in the core tag library:

    <c:set var="contains" value="false" />
    <c:forEach var="item" items="${myList}">
      <c:if test="${item eq myValue}">
        <c:set var="contains" value="true" />
      </c:if>
    </c:forEach>
    

    After this runs, ${contains} will be equal to "true" if myList contained myValue.