Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with Map backed ActionForm

 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a fairly simple JavaBean class to represent a business object. I also have an ActionForm which needs to hold the values for several of these beans, and I've decided to use a series of Maps to hold the properties in the ActionForm. The Map key is a String property from the business object.

For example, I need to present properties from several Students on a single form. So I have:



In the JSP I have:



The ActionForm and JSP work, I just can't find a good way to reconstruct the set of students using BeanUtils or PropertyUtils in the target action when the form is submitted. At present I have all sorts of loops etc, and copying out the properties individually.

Please tell me I'm missing something and there's a MUCH better way of doing it!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me I'm missing something and there's a MUCH better way of doing it!


Ok, you asked for it. Get rid of all the HashMaps. I assume you've already created a Student object that contains all the properties (age, gender, surname,etc.). Fine. Keep that and create a property on your ActionForm bean named "students" of type java.util.List. This property will contain an ArrayList of all the Student objects you wish to work with on the form. Then use "indexed properties" to access each student. This link will give you some good pointers on using indexed properties.

Then in your Action class, all you have to do is iterate through the List of Student objects to get the updated properties for each Student.
 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was quick!

Thanks very much for the advice - I hadn't considered using anything other than simple properties in the Actionform.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic