Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4016
  • Last Modified:

Iterate over Hashmap in JSP - Struts

I have a Bean class(myTO - Value Object with getters and setters) which is stored in a LinkedHashMap (called masterMap).
myTO(value object) has a Hashmap inside it.

Finally I've placed the masterMap in the request scope.

Can any one suggest me how to iterate the myTO(which is inside masterMap) , along with corresponding HashMap inside myTo (in JSP-Struts)?

I was able to go one level. i.e, iterate the myTO inside masterMap. But couldnt itterate the Hashmap insde the myTO(have to itterate using a key which is stored in a string array)

Sorry, If the Question is confusing :(
1 Solution
sounds like you need the key, otherwise how can you access the map

I have done something similar (I think).  

Let me understand exactly what you are doing...

you have a LinkedHashMap called masterMap, which holds a collection of MyTO objects.  These objects also contain a HashMap which you want to iterate over?  If that is correct you can try the following:

1) retrieve the masterMap from the request.
2) retrieve the set of keys from masterMap using masterMap.keySet();
3) iterate through the keys retrieving the myTO object.
4) for each myTO object, retrieve the HashMap.
5) again, use the keySet method of HashMap to get a Set of the keys.
6) iterate through the keys to retrieve the values/object associated.

example code:

LinkedHashMap<String, myTO> masterMap = (LinkedHashMap<String, myTo>)request.getAttribute("masterMap");

Set<String> keysForMasterMap = masterMap.keySet();
Iterator<String> keysIterator = keysForMasterMap.iterator();


String key = keysIterator.next();
MyTO myTO = masterMap.get(key);

HashMap<String, XXXX> mapFromMyTO = myTO.getXXXX();

Set<String> keysFromMyTOMap = mapFromMyTO.keySet();
Iterator<String> myTOMapKeysIterator = keysFromMyTOMap.iterator();

String keyForObject = myTOMapKeysIterator.next();
ObjectXXXX objectRetrieved = mapFromMyTO.get(keyForObject);

//Now you can do something with the object you have retrieved.


110210Author Commented:
Phoenix101- I want to do it in JSP, using <logic:..> tags and not in Java.
look at here it might help you..


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now