Warm tip: This article is reproduced from stackoverflow.com, please click
java jsp jstl

Evaluate list.contains string in JSTL

发布于 2020-04-13 10:34:01

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.

Questioner
OscarRyz
Viewed
159
2012-01-10 12:12

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.