Html form fields as array elements in layout form

I would like to add dynamic HTML input fields on one page, and each of them corresponds to the struts array property element. Say I have several identical fields on an HTML page:

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

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

FormFile [] myfile;

Obviously this does not work, but I am looking for how to do something equivalent.

EDIT: The above does not work for uploading files and only of type FormFile.

Otherwise, the array element is intuitively mapped to an input element or an html: text element. So, to clarify my question, why can't I load an array of files using struts?

+4
source share
2 answers

OK, here is the “will work for me” solution:

First of all, in your html / jsp file, the name attribute must 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 by the number of elements actually served, i.e. check the zero elements of the array. I think this provides a good basis for adjusting it to your needs. As I said in the final editing of my question, html: text> or <input type = "text"> you will not need either an indexed property or array initialization in your bean form. I really don’t know why this is so, I looked for it a bit in the class org.apache.commons.beanutils.PropertyUtilsBean in the apache commons project: anyone can take a look at the set * Properties of this class.

+2
source

what you are looking for is called indexed properties. Since it’s better “as is” than I can describe it, take a look here .

+1
source

All Articles