Loop through Hashmap in Struts 2

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Loop through Hashmap in Struts 2

hulo
Hi guys, I am new to Struts 2.

I have an Nested HashMap created in one of my Action class. I would like to display the data stored in the hashmap in JSP page. I am just wondering how to loop through the Hashmap, so I can grab all its contents.

The structure of my HashMap is following:
1 Nested Hashmap
2 Nested Hashmap
3 Nested Hashmap
4 Nested Hashmap
5 Nested Hashmap

Inside the Nested Hashmap is  
Name   Tom
Age     20
Sex     M


What I am trying to do here is to grab student information from database and display them on the page, Is there any better of way of doing this than like the above putting them in the hashmap first after database retrieval happend?


Thanks guys!!!


Reply | Threaded
Open this post in threaded view
|

Re: Loop through Hashmap in Struts 2

DustFrog
First, if your students are in some order, it may be better to store your NestedHashMap in an ArrayList or a LinkedHashMap.

Anyway, for this example, let's assume you have an action method called "getStudents()" where it returns your HashMap<Integer, NestedHashMap>.

You can use the Struts2 iterator tag like this:

<s:iterator id="student" value="students">
  Name:  <s:property value="#student.Name" /><br />
  Age:  <s:property value="#student.Age" /><br />
  Sex:  <s:property value="#student.Sex" /><br />
  <br />
</s:iterator>

FYI, I'm using syntax from my experience with Struts 2.0.14.  This may be different in the 2.1.x versions.
Reply | Threaded
Open this post in threaded view
|

Re: Loop through Hashmap in Struts 2

Summa
Hi DustFrog,

Once we display in jsp and if we make changes to the fields , how do we update them or send them back to the action class ? can you please let me know?

Thanks
Summa