在struts 2中将可变数量的参数从表格传递到动作 [英] Passing variable number of parameters from form in to action in struts 2
问题描述
我在我的应用程序中使用struts2,并且在jsp中有一个表单,该表单已提交给动作类.
I am using struts2 in my application and have a form in a jsp which is submitted to a action class.
表单中输入字段的数量在运行时可能会有所不同,因此操作类不能为参数设置固定的setter和getter.我试图在单个动作类中捕获这些可变数量的输入.我想知道是否可以通过Struts 2进行操作,如果可以的话,该怎么办?我知道表单中的输入字段名称(通过jsp表单中的迭代器填充的输入字段名称).
The number of input fields in the form can vary at runtime and hence the action class can not have fixed setter and getter for the parameters. I am trying to capture these variable number of inputs in a single action class. I want to know if its possible via Struts 2 and if it is how can i do it ? I am aware of the input field names in the form (input field names populated via iterator in the jsp form).
注意:1)我知道通过针对动作类中所有表单值的适当的getter和setter来捕获动作类中的表单值(参数)的方法. 2)我已经按照struts 2 http:中所述进行了模型驱动的操作: //struts.apache.org/2.3.1/docs/model-driven.html
Note: 1) I am aware of the way to capture the form values (parameters) in the action class via the appropriate getter and setter for all form values in the action class. 2) I have gone through the model driven action as described in struts 2 http://struts.apache.org/2.3.1/docs/model-driven.html
我在搜索方面无法获得任何解决方案,我们将不胜感激.
I could not get any solution on searching and any help is appreciated.
表格人口代码:
<s:form action="/reports/getReport.action" cssClass="table_with_padding">
<s:iterator value="reportParamsList.items" id="paramList_item">
<tr><td><s:property value="#paramList_item.paramdesc" /></td><TD><s:textfield name="#paramList_item.paramname" /></TD></tr>
</s:iterator>
<s:submit theme="ajax" loadingText="%{getResource('SiteWide.Loading.Text')}" targets="app_area" type="button" align="left" cssClass="app_form_button" value="Submit" />
</s:form>
推荐答案
对所有textfields
使用相同的name
属性,并使用相应的hidden
字段进行识别
Use same name
attribute for all textfields
and a corresponding hidden
field to identify it
<s:iterator value="reportParamsList.items" id="paramList_item">
<tr>
<td>
<s:property value="#paramList_item.paramdesc" />
</td>
<td>
<s:textfield name="element" />
<s:hidden name="myValue" value="#paramList_item.paramname" />
</td>
</tr>
</s:iterator>
然后在您的动作类中声明这些变量及其getter/setter方法
Then in your action class declare these variables and their getter/setter
private List<String> element;
private List<String> myValue;
现在您可以遍历myValue
列表并获取其相应的textfield
值
Now you can iterate over myValue
list and get its corresponding textfield
's value
Iterator<String> it = myValue.iterator();
int index = 0;
while(it.hasNext()){
System.out.println("hidden field's value="+it.next());
System.out.println("textfield's value="+element.get(index));
index++;
}
这篇关于在struts 2中将可变数量的参数从表格传递到动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!