File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Iterating a hashmap in jsp using struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Iterating a hashmap in jsp using struts2" Watch "Iterating a hashmap in jsp using struts2" New topic

Iterating a hashmap in jsp using struts2

Ankush Sharma

Joined: Sep 12, 2009
Posts: 3

I am trying to create a dynamic form while iterating an arraylist of entity objects.display fields are hashmap values where I should be able to edit the hashmap fields hashmap<String,value an Object) and submit the form values back.I am able to create the dynamic form but when I submit,cant get the values back in my Action class,as Arraylist becomes empty while submitting my form.I am using struts2 in my application

Any better solution for iterating a hashmap and getting the complete hashmap in the Action class.
Miltos Tereres
Ranch Hand

Joined: Sep 04, 2009
Posts: 32
Google it!
HashMaps i think by using an iterator and .key and .value property ! With OGNL expressions i think.!Not too hard
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

Hi Ankush, welcome to javaranch.

The problem as I understand is that you want the HashMap to be available to the action that receives the request after you submit the form. Well this will not happen automatically for you. You can put the HashMap into the session and access it in the target Action. BTW what are the values stored in your HashMap, are they objects of a specific class??

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton

Joined: Sep 29, 2008
Posts: 12617

How are you declaring the map in the action you're submitting to?

@Ankit: I'm not sure what you're saying; maps can be passed to JSP as an action property, just like simple types, and populated from form submission if the form element names are written using map syntax.
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

@David, what I meant was that suppose that if he has a map which contains arbitrary values (as he said that the map is <String,Object>), and he wants the Map to render the JSP and the Next Action to have that map, then he can put it in session so that the next Action can get it out of the session. And if the objects are of a particular type, like lets say Employee, then he can do what you are saying, or implement a custom type converter if for example only the employee's ID is in the form, then the custom Type converter can be used to convert that ID into Employee automatically. That's why I asked what are the objects stored in the HashMap as values. I hope I'm not off the track and I've understood the problem correctly...
I agree. Here's the link:
subject: Iterating a hashmap in jsp using struts2
It's not a secret anymore!