0

不使用 DynaForm 和它的亲戚。

我想使用 POJO 数据传输对象,例如 Person:

public class Person {
   private Long id;
   private String firstName;
   private String lastName;
   // ... getters / setters for the fields
}

在 struts 实景表单中,我们将拥有:

public class PersonUpdateForm extends SLActionForm {
   String organization;
   Person[] persons; // all the people will be changed to this organization; they're names and so forth can be updated at the same time (stupid, but a client might desire this)

   // getters / setters + index setters / getters for persons

}

相应的 html:text 标记在 JSP 中会是什么样子以允许这样做?如果我切换到 List Persons 字段并使用延迟加载列表(在 commons-collections 中),那将如何改变thingsg?

在 struts-1.2(.9?) 中似乎没有很好的方法来做到这一点

非常感谢所有帮助!!!如果您需要更多上下文,请告诉我,我可以提供一些。

4

1 回答 1

1

好吧,我相信我已经想通了!诀窍是让您的索引 getter 每次由 BeanUtils 的 populate 方法调用 getPersons() 方法时创建一个元素。代码已经完成,但我得到了一个积极的结果。现在是 3:30,我已经坚持了一段时间。似乎没有人知道答案,这让我想用鳟鱼打他们的头。至于我自己的无知……只能怪他们!

public List<Person> getPersons() {
   persons.add(new Person()); // BeanUtils needs to know the list is large enough
   return persons;
}

当然,也要添加索引的 getter 和 setter。

我记得我实际上是如何做到这一点的。您必须将上述人员列表预初始化为您希望传输的最大大小。这是因为首先将 List 转换为数组,然后在数组的每个元素上设置属性,最后使用 setPersons(...) 设置回 List。因此,使用延迟加载 List 实现或类似方法(例如上面显示的)将不适用于 struts live。以下是您需要更详细地执行的操作:

private List<Person> persons = new ArrayList<Person>(MAX_PEOPLE);
public MyConstructor() { for(int i = 0; i < MAX_PEOPLE; i++) persons.add(new Person()); }

public List<Person> getPeopleSubmitted() {
    List<Person> copy = new ArrayList<Person>();
    for(Person p : persons) {
        if(p.getId() != null) copy.add(p); 
        // id will be set for the submitted elements;
        // the others will have a null id
    }
    return copy; // only the submitted persons returned - not the blank templates
}

这基本上就是你必须做的!但真正的问题是 - 谁在使用 struts live 了?!

于 2009-04-27T07:33:45.033 回答