Populate Collection from Struts2 Form Submission(从 Struts2 表单提交中填充集合)
问题描述
我正在尝试从表单中填充 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 表单提交中填充集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Struts2 表单提交中填充集合
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
