我正在尝试从表单中填充 bean 列表:
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 表单提交中填充集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!