File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Iterating over Map using JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Iterating over Map using JSTL" Watch "Iterating over Map using JSTL" New topic
Author

Iterating over Map using JSTL

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374


Why it doesn't work? I want to iterate over list of each map entry.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Neither this helping me,



The first one is wrong but how about second. map[key[0]] should return me List<Object[]> and should iterate over that list and so deatils should have Object[] and I am accessing one of the element using details[1].

Please advise why it is not working. I am not getting any error.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

Where are you defining the list key?

Why the [0] in the first place? What you are doing is rather odd.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Bear Bibeault wrote:Where are you defining the list key?

Why the [0] in the first place? What you are doing is rather odd.


1) It is defined in Java class too.
2) and it is List<Object[]> and I have saved 'key' on key[0]

So I am passing two things ,



and I am having two JSTL for loops. First loop will give me keys one by one which I am getting right now. and that value will go as a Key to Map so that it will give me List and I want to iterate over that list and that list contains Object[] and [1] element of that array is having the value that I want and that is why I wrote ${detail[1]} .

Yes, it seems like little bit complex. but this is how I see the data structure.

EDIT : Information updated. By mistake I wrote two dimensional array.
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

You simply need to nest the <c:forEach> tag ... put the map in scope .. get the values from the map ... put the resulting values collection in scope (foreach works with collections) .. iterate over the map values ... then iterate over the object array.

I'll write small example in a few minutes ...


kf
SCJP 5.0 (preparing for SCWCD)
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Keith Flo wrote:You simply need to nest the <c:forEach> tag ... put the map in scope .. get the values from the map ... put the resulting values collection in scope (foreach works with collections) .. iterate over the map values ... then iterate over the object array.

I'll write small example in a few minutes ...


Keith, thank you for your reply.

- My for loops are nested.
- Map and List both are in scope.

Algorithm is like this,

- First for loop iterates over List <Object[]> where Object[0] has key
- In nested for loop, I am trying to get List<Object[]> which is value for that Map. and will iterate over that list to get the value.




I am neither getting error nor result.
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

Correction ... just place the collection of map values in scope .. you dont need the map or to place the values collection in scope. You only need one object in scope ... the collection of map values.



Give it a try!
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

I see ..

I'll adjust the example in harmony with the code you provided ..

I've gotta run and do something .. I'll try to post the adjusted example in about 30 minutes ...
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

Here it is ...



This produces a table row for each key in the map and a drop-down list (html <option> tag) for each item in the Object array.
I tried this in tomcat and it works fine ... this was a little bit of head scratcher ... the key point is you can get both the map keys and values from the map. ..... 'mapname.key' returns the key and 'mapname.value' returns the value

hope this helps ...
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hey Keith,

You're great. You saved me.

Thank you very much.

Just for the curiosity I want to ask, our approaches were same logically right? then why my code neither worked nor gave any error? Is anything wrong with my code?

Again thank you very much.
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

No problem ... happy to help

Yes ... your approach was correct but you got some of the details a little confused.

For example ... your original code snippet has a Map that contains a List that contains an Object array.



You just needed the Map and the Object array. Also, in the forEach tag you were trying to use Java syntax. Remember that JSP EL was supposedly designed with non-programmers in mind (personally I don't think its very friendly for non-programmers) ... so the syntax in the JSP JSTL forEach loop is a little different.

You had ...



it should have been ...



'var' is the name of the variable that holds each item in the Map or collection as you iterate ..
'items' is the Map or Collection (array, List or Set) you want to iterate over.
if 'items' is a Map (HashMap or LinkedHashMap), then the map.key is the value of the each key as you iterate and map.value is the value.


>
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks Kieth.I had the same problem and this thread helped me fix my probelm..... Thanks a lot again


Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Keith Flo
Ranch Hand

Joined: Nov 29, 2005
Posts: 128

Its nice to see an old post is still useful!

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

Larray Evanson,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterating over Map using JSTL