PDA

View Full Version : SimpleFormController with a variable number of backing objects?


rwerner
Jun 5th, 2006, 08:49 PM
Is there a way to have a SimpleFormController with a variable number of backing objects?

For example, let's say I have a Form that represents an Attribute. An Attribute object is basically just a name-value pair. In my Velocity template I use #springBind("attribute.*") and I have a hidden field representing the attribute name and a text field representing the attribute value. This is easy enough.

But let's say I want my Form to represent two Attribute objects. I could have my Form Backing Object be some container class that contains a getAttr1() method and a getAttr2() method. In my Velocity template I'd then use #springBind("container.attr1.*") for the first hidden field/text field pair of inputs, then #springBind("container.attr2.*") for the second pair of inputs. Then when I submit the form, the two pairs get mapped successfully to the two Attribute objects stored within the container class.

But what I really want is the ability to dynamically have as many attributes in my Form Backing Object as necessary. For example, in my Velocity template I'd have a loop that generates as many hidden field/text field pairs of inputs as determined from an external value, and then when submitting to the Form controller, the container object would contain an array or a list of Attribute objects corresponding to however many pairs of inputs I had in my submitting page.

Is it possible to do this within Spring's framework? Obviously I could bypass the FormController objects altogether and simply have a controller manually parse the incoming POST parameters and create the array of Attributes by hand, but it seems like Spring would have a way to do all this automatically. Any suggestions?

Thanks in advance!