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.
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.
works nicely if the list is small. Just realize there is a performance cost to doing it this way.
Yeah, there would be if you get high enough. I've used it for collections of 10-20 things and have not experienced any performance issues. The thing I think is worse is the number of lines of JSTL. Still, I think this is the only way without setting up your own TLD (which isn't too difficult and may very well be worth it).