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 Display of hashmap values using jstl Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Display of hashmap values using jstl" Watch "Display of hashmap values using jstl" New topic
Author

Display of hashmap values using jstl

Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 95
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



SCJP 6.0
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Of course, you need to iterate over the list.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Also, be careful that looping through a Map does not guarantee the order in which values are retrieved.


[My Blog]
All roads lead to JavaRanch
Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 95
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Feb 23, 2009
Posts: 95
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Feb 23, 2009
Posts: 95
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

Joined: Nov 24, 2005
Posts: 14688
    
  16


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

Joined: Feb 23, 2009
Posts: 95
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display of hashmap values using jstl