【发布时间】:2012-03-26 13:04:24
【问题描述】:
我在 Struts2 中填充用户定义的对象属性列表时遇到问题。
这是我的例子(getters / setters 省略):
公共类 Foo { 私有字符串 attr1;私有字符串 attr2; }
public class Bar { 私有列表 foos; }
public class StrutsAction extends ActionSupport { private Bar bar; }
我在 JSP 中有如下代码(摘录):
<tr><td><input type="text" name="bar.foos.attr1"/></td><td><input type="text" name="bar.foos.attr2"</td></tr>
<tr><td><input type="text" name="bar.foos.attr1"/></td><td><input type="text" name="bar.foos.attr2"</td></tr>
我需要每个表行在 List 中为每个属性创建 1 个 foo 项,但是我的代码在传递时为每个属性创建一个新的 foo 对象,我最终得到 4 个 foo 而不是 2 个!
我知道我可以通过将索引硬编码到 html 中来解决问题,如下所示:
<tr><td><input type="text" name="bar.foos[0].attr1"/></td><td><input type="text" name="bar.foos[0].attr2"</td></tr>
<tr><td><input type="text" name="bar.foos[1].attr1"/></td><td><input type="text" name="bar.foos[1].attr2"</td></tr>
我想知道是否可以在不对索引进行硬编码的情况下完成?
【问题讨论】:
标签: java list collections struts2 ognl