1

I have a JSP that allows users to dynamically create additional form fields to create multiple objects. Perhaps I want to allow users to be able to submit as many line items as they want when submitting an invoice form.

How do I create a Struts 2 Action that will be able to take in an ArrayList populated with objects created from those dynamically generated fields.

deHaar
  • 17,687
  • 10
  • 38
  • 51
Jaryl
  • 2,561
  • 1
  • 24
  • 33

2 Answers2

1

You should read the Tabular input guide.

Damien B
  • 1,992
  • 15
  • 19
1

According to the (ever-poor) documentation, which forces you to try to extrapolate the information you want, rather than just telling you authoritatively (and assuming you're really asking about Struts' built-in type conversion), your form fields would need to be named something like...

someList.makeNew(0).someField1
someList.makeNew(0).someField2
...
someList.makeNew(1).someField1
someList.makeNew(1).someField1
...

...and you would then need to set up an ActionClassName-conversion.properties file to let the type converter know how to handle type conversion for fields which begin with someList.

The only time I actually tried this myself, I had trouble getting it working with Lists and ended up having to use Maps.

Here's a useful blog entry about modifying a Map of objects using type conversion - I haven't had much luck finding useful information about the makeNew field name format the documentation mentions, but this might help you get started.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Jonny Buchanan
  • 61,926
  • 17
  • 143
  • 150