aspose file tools*
The moose likes JSP and the fly likes Getting values from a map in JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Getting values from a map in JSTL" Watch "Getting values from a map in JSTL" New topic

Getting values from a map in JSTL

Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375
I spent a lot of time with this problem: I have map of values in the request, I'm iterating over another collection and I want to get values from the map with keys I got from the collection.
Something like:
<c:forEach items="${myCollection}" value="elem" >
<cut value="${myMap[elem.key]}" />
but no way 'myMap[elem.key]' doesn't work!
I've tried various combinations, without luck.
(finally I did it by iterating over the map and comparing the key, but that should be the map's work!)
Somebody saw something like this?

Software Surgeon
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60049

"It doesn't work" is not a useful description of the problem.

Your syntax is fine. Have you verified that the map is really in the request? Have you verified that elem.key contains what you expect on each iteration?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375
At least I'm glad the sintax seems fine
I gave up when I reached the point where I had:
<c:set var="aKey" value="<some key I know is in the map>" />
<cut value="${aKey == elem.key} /> <-- result: true
<cut value="${myMap[aKey]}" /> <--- result: the value I expected
<cut value="${myMap[elem.key]}" /> <--- result: empty!!

I'll keep experimenting, because I'm quite puzzled...
I agree. Here's the link:
subject: Getting values from a map in JSTL
Similar Threads
c:forEach in JSTL
EL mapEntry.value prints Ljava.lang.String@
Accessing a HashMap using Struts 2
Strurs 2 iterate over a Map
Iterating over Map using JSTL