Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Iterating over a map with JSTL

Thread view: 
Christine Mayer - 30 Aug 2007 15:46 GMT
Hi,

I've got an object that contains 2 attributes - a String and a map,
which contains a set of Strings.
I want to iterate over these objects, and so I also want to iterate
over the map, and print out key and value.
I've read this wasn't possible and one should instead iterate over the
values, something like:
<c:forEach var="loop" items="${hashmap.values}"

But that just isn't what I need - I need both, key and value. I know I
could also just as well create a proprietary object that contains 2
Strings each - but imho in this case this is just too much overhead.

What can you recommend me to do? Maybe I should write a method into my
bean "getNextKey" and "getNextValue - the problem is if someone would
call "getNextKey" on the view twice then the keys and values wouldn't
mach anymore - in otherwise I would like to leave the control of the
map to Java...

Thanks in advance,

Christine
Daniel Pitts - 30 Aug 2007 16:26 GMT
> Hi,
>
[quoted text clipped - 19 lines]
>
> Christine

have you tried (this is untested)
<c:forEach var="entry" items="${hashmap}">
 ${entry.key} = ${entry.value}
</c:forEach>

Or, if you can iterator over the keySet(), you can get the value that
corrisponds to that key: <c:forEach var="key" items="$
{hashmap.keySet}"> ${key] = ${hasmap[key]}

I haven't tested these, so they might not work. Let me know if they do.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.