Looking for a Collection taglib

J

John Fereira

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.
 
C

Cid

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:eek:therwise>
Didn't find it.
</c:eek:therwise>
</c:choose>
 
J

John Fereira

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:eek:therwise>
Didn't find it.
</c:eek: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:eek:therwise>
Didn't find it.
</c:eek: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:eek:ut tag. If I "hard code" the
string key (i.e. myCollection['1000']) it works.
 
J

John Fereira

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:eek:therwise>
Didn't find it.
</c:eek: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:eek:therwise>
Didn't find it.
</c:eek: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:eek: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:eek:ut value="${document.title}"/>
</td>

<td>
<c:set var="thisKey"><c:eek:ut value="${document.documentID}"/></c:set>
<c:set var="thisSubInfo">
<c:eek: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:eek:therwise>
<img alt="" src="<c:url value="${images}/unchecked.gif"/>" >
</c:eek: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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top