• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Display of hashmap values using jstl

 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Below is the code that I am working on. I am trying to get the values that are stored in the map using JSTL. If the map values are string and integer I am able to get the value. But I have another value that is array stored in a list. How would i get the values from it using jstl?



My output


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, you need to iterate over the list.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, be careful that looping through a Map does not guarantee the order in which values are retrieved.
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Also, be careful that looping through a Map does not guarantee the order in which values are retrieved.


So how do I make sure that I am only doing an extra iteration for the list and not for the string and integer?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a tough one. There's obviously no instanceof capability. The basic question becomes, why are you creating such an unfriendly construct in the first place?

The rule of thumb os to use constructs that are JSTL and EL-friendly, rather than trying to do contortions on the JSP to accommodate constructs that are difficult.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better EL-friendly approach would be to have a class holding the name, the id and the (book?) list. Than have a list holding the instances of that class. The order would be guaranteed. Let's assume that the "authors" list contains such instances:

(there will be an extra comma after the last book)
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bear,

Sometimes you never choose to do such constructs.... but I am left with that situation, this is the answer for your basic question. Is there any other way out?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could write a custom EL function to test the value to see if it is a List implementation or not.
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bear and Christophe,

Thanks for your input. I really appreciate it. I have a solution please do let me know if it work. What if I have the list as the key and string and integer has the value. I have tried the code, only thing I am not able to get the output that I am expecting. Need some help.



This is the output that I get


What I am expecting is



Seems I have to loop through the values first then the key. I tried, but not able to get what I exactly want, any help please





 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't understand. Why do you want to use a the array as a Map key ? Moreover, you are overwritting the same key's value here, so you'll never get "A" back again. Can you explain what you are trying to do ?
 
Romeo Ranjan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:There's obviously no instanceof capability.


Thanks I solved the issue. Christophe thanks for pointing out that I was using hash map, so ulimately it overrides. I used list as a key because as Bear has mentioned there is no instance of capability so it was hard for me to identify if it was a list or string or integer. But then having list as a key I just had to loop through the key once, and next loop the values alone,which was just one looping, indefinite of the type of the value stored in the map.

Thanks Bear and Christophe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic