html表单字段作为struts形式的数组元素

时间:2021-06-07 05:01:57

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:

我想要做的是在一个页面中添加动态HTML输入字段,每个字段对应一个struts数组属性的元素。假设我在HTML页面中有许多相同的字段:

< 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:

并且在提交表单时,我希望每个字段对应于struts表单bean中FormFile数组中的元素:

FormFile [] myfile;

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.

编辑:以上不适用于上传文件和仅FormFile类型。

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?

否则,直观地将数组元素映射到input元素或html:text元素实例。那么,为了使我的问题更具体,为什么我不能用struts上传一个文件数组?

2 个解决方案

#1


2  

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

好的,这是一个“能为我工作”的解决方案:

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

首先,在你的html / jsp文件中,应该为name属性编制索引:

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

文件1:

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

文件2:

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

文件3:

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.

struts表单中的“catch”是初始化FormFile数组。做这两件事就可以了。请注意根据实际提交的元素数检查数组的长度,即检查空数组元素。我认为这为根据您的需求调整它提供了一个很好的基线。正如我在我的问题的最后编辑中所说,对于 元素,您在表单bean中既不需要索引属性也不需要数组初始化。我真的不知道它为什么,我在apache commons项目中的org.apache.commons.beanutils.PropertyUtilsBean类中查了一下:任何有兴趣的人都可以看一下该类的set * Property方法。

#2


1  

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.

您正在寻找的是“索引属性”。由于有比我描述的更好的“怎么样”,请看一下这里。

#1


2  

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

好的,这是一个“能为我工作”的解决方案:

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

首先,在你的html / jsp文件中,应该为name属性编制索引:

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

文件1:

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

文件2:

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

文件3:

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.

struts表单中的“catch”是初始化FormFile数组。做这两件事就可以了。请注意根据实际提交的元素数检查数组的长度,即检查空数组元素。我认为这为根据您的需求调整它提供了一个很好的基线。正如我在我的问题的最后编辑中所说,对于 元素,您在表单bean中既不需要索引属性也不需要数组初始化。我真的不知道它为什么,我在apache commons项目中的org.apache.commons.beanutils.PropertyUtilsBean类中查了一下:任何有兴趣的人都可以看一下该类的set * Property方法。

#2


1  

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.

您正在寻找的是“索引属性”。由于有比我描述的更好的“怎么样”,请看一下这里。