This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

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: 63198

"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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Getting values from a map in JSTL
It's not a secret anymore!