代码之家  ›  专栏  ›  技术社区  ›  les2

在struts中(使用struts live)进行多行更新的好方法是什么?

  •  0
  • les2  · 技术社区  · 17 年前

    没有使用DynaForm和它的亲属。

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

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

    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
    
    }
    

    JSP中相应的html:text标记是什么样子的?如果我切换到List persons字段并使用惰性加载列表(在commons集合中),这将如何改变thinsg?

    1 回复  |  直到 17 年前
        1
  •  1
  •   les2    16 年前

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

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

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

    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?!