tags:

views:

3190

answers:

2

What I would like to do is add dynamically HTML input fields in one page, and each one of them correspond to an element of a struts array property. Let's say I have a number of identical fields in an HTML page:

< input type="file" name="myfile" />

and when the form is submitted, I want each field to correspond to an element in a FormFile array in the struts form bean:

FormFile [] myfile;

Obviously the above doesn't work, but I am looking for how to do something equivalent.

EDIT: The above doesn't work for uploading files and the FormFile type only.

Otherwise, an array element is mapped to an input element or html:text element instance, intuitively. So, to make my question more specific, why can't I upload with struts an array of files?

+1  A: 

Hey,

what you are looking for is called "indexed properties". Since there are better "how to's" than I can describe it, take a look here.

Cheers, mana

mana
Indexed properties seem like a good direction. I am skeptical about the ability to actually submit data as array elements. I also have to adopt the struts documentation examples to work in my code which I have failed so far.
atas
+2  A: 

OK, here is a "would work for me" solution:

First of all, in your html/jsp file the name attribute should be indexed:

File 1: < input type="file" name="myfile[0]" />

File 2: < input type="file" name="myfile[1]"/ >

File 3: < input type="file" name="myfile[2]" />

The "catch" in your struts form is to initialize the FormFile array. Doing these two things will do the job. Just be careful to check the length of the array against the number of elements actually submitted, i.e. check for null array elements. I think this provides a good baseline for adjusting it to your needs. As I said in the final edit of my question, for a < html:text > or < input type="text" > element you would need neither an indexed property nor an array initialization in your form bean. I don't really know why it is, I looked it up a bit in org.apache.commons.beanutils.PropertyUtilsBean class in apache commons project: anyone interested might take a look at the set*Property methods of that class.

atas