2009-04-27 36 views
0

沒有使用DynaForm和它的親屬。什麼是在struts中進行多行更新的好方法(使用struts live)?

我想用一個POJO數據傳輸對象,例如,聯繫人:

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 

} 

會是什麼相應的HTML:文本標籤看起來像在JSP中允許這樣做?如果我切換到List persons字段並使用延遲加載列表(在commons-collections中),那麼這將如何改變呢?

似乎是在struts-1.2要做到這一點沒有什麼好辦法(0.9?)

所有幫助是極大的讚賞!如果你需要更多的上下文讓我知道,我可以提供一些。

+0

好的,我相信我已經想通了! 訣竅是每次使用BeanUtils的填充方法調用getPersons()方法時,索引getter都會創建一個元素。 代碼已完成,但我得到了一個積極的結果。 現在是3點半,我一直在這一段時間陷入困境。似乎沒有人知道答案,這讓我想用鱒魚把它們砸在腦袋裏。至於我自己的無知......我只責怪他們! – les2 2009-04-27 07:31:47

回答

1

好的,我相信我已經想通了!訣竅是每次使用BeanUtils的填充方法調用getPersons()方法時,索引getter都會創建一個元素。代碼尚未完成,但我得到了一個積極的結果。現在是3點半,我一直在困擾這一點。似乎沒有人知道答案,這讓我想用鱒魚把它們砸在腦袋裏。至於我自己的無知......我只責怪他們!

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

當然也可以添加您的索引獲取器和設置器。

我記得我是怎麼做到的。您必須將上述人員列表預先初始化爲您希望傳輸的最大大小。這是因爲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?!

相關問題