• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Iterating over Map using JSTL

 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why it doesn't work? I want to iterate over list of each map entry.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you defining the list key?

Why the [0] in the first place? What you are doing is rather odd.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


>
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kieth.I had the same problem and this thread helped me fix my probelm..... Thanks a lot again
 
Keith Flo
Ranch Hand
Posts: 128
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its nice to see an old post is still useful!

 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Larray Evanson,
Your post was moved to a new topic.
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic