I've been developing an application using the Spring Framework and
have been using the JSTL core tag library for iterating through some
lists. The forEach tag does a nice job of iterating through a List,
Hashtable, TreeMap, etc but doesn't (at least that I can tell) support
the "contains" method. That is, I would like to be able to pass a
Collection to a JSP and determine whethor or not the Collection
contains a specific key.
I know that this can be done by writing a Custom tag library but I'm
hoping that someone has already done it.
How about something like:
-------------------------------------------
<%@ taglib uri="
http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:choose>
<c:when test=${myCollection['someKey'] != null}>
Found it
</c:when>
<c

therwise>
Didn't find it.
</c

therwise>
</c:choose>
That's close to what I want. I actually was approaching it the same
way but just using <c:if >
However, I need to substitute 'someKey' with an expression so that it
looks like this...
<c:choose>
<c:when test="${myCollection['${document.id}'] != null}">
Found it
</c:when>
<c

therwise>
Didn't find it.
</c

therwise>
</c:choose>
but I can't seem to get the syntax right. I know that ${document.id}
is set correctly because I can display it with a <c

ut tag. If I
"hard code" the string key (i.e. myCollection['1000']) it works.
Okay, I figured it out...
Here's the code that worked...
================================
<table width="580">
<c:forEach var="document" items="${documents}">
<tr>
<td>
<c

ut value="${document.title}"/>
</td>
<td>
<c:set var="thisKey"><c

ut value="${document.documentID}"/></c:set>
<c:set var="thisSubInfo">
<c

ut value='${userSession.subscriptionMap[thisKey].documentID}'/>
</c:set>
<c:choose>
<c:when test='${thisSubInfo == thisKey}'>
<img alt="" src="<c:url value="${images}/checked.gif"/>" >
</c:when>
<c

therwise>
<img alt="" src="<c:url value="${images}/unchecked.gif"/>" >
</c

therwise>
</c:choose>
</td>
</tr>
</c:forEach>
</table>
================================
I tried replacing "thisKey" with document.documentID but it didn't work.
Note that when setting thisSubInfo that the expression within the brackets
is not quoted or specified as ${thisKey}. Note that I also test that the
strings match and not for a null value. Apparently the c:set tag was
setting thisSubInfo to an empty string if the subscriptionMap did not
contain the key.