从Struts2表单提交中填充集合 [英] Populate Collection from Struts2 Form Submission
本文介绍了从Struts2表单提交中填充集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从表单中填充豆子列表:
I'm trying to populate a List of beans from a form:
public class Foo {
public String attr1;
public String attr2;
}
public class Bar {
public List<Foo> foos;
}
public class StrutsAction extends Action {
public Bar bar;
}
所以在我的Struts2表单中,填充Foo的最佳方法是什么?凭直觉,我想做:
So in my Struts2 form, what's the best way to populate Foo? Intuitively, I want to do:
<input type="hidden" name="bar.foos.attr1" />
但这不起作用,并且会导致冲突.我确信答案非常简单,而且我忽略了它.
but that isn't working and would cause collisions. I'm sure the answer is very simple and I'm overlooking it.
推荐答案
如果我理解正确,您只是想为每个隐藏字段使用不同的名称吗?
If I understand it correctly, you just want different name for each hidden field?
<s:iterator value="bars" status="key">
<s:hidden name="bar.foos[%{#key.index}].attr1" value="attr1" />
<s:hidden name="bar.foos[%{#key.index}].attr2" value="attr2" />
</s:iterator>
应该给您等同的
<input type="hidden" name="bar.foos[0].attr1" value="some value" />
<input type="hidden" name="bar.foos[0].attr2" value="some other value" />
<input type="hidden" name="bar.foos[1].attr1" value="some value" />
<input type="hidden" name="bar.foos[1].attr2" value="some other value" />
如果您有适当的getter/setter,则应在提交表单时设置所有值.
If you have proper getter/setter, it should set all the values when the form is being submitted.
这篇关于从Struts2表单提交中填充集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文