2012-05-24 32 views
2

我正在使用Struts2。我在pojo中有一個hashset。我正在嘗試將值提交給哈希集。我無法將收藏類型更改爲列表。struts2中hashset的設置值

這裏是POJO

Item{ 
Set<Person> personCollection; 
long itemCode; 

    public void setItemCode(long itemCode) 
    { 
     this.itemCode=itemCode; 
    } 
    public long getitemCode() 
    { 
     return itemCode; 
    } 
    public void setPersonCollection(Set<Person>personCollection) 
    { 
     this.personCollection=personCollection; 
    } 
    public Set<Person> getPersonCollection() 
    { 
     return personCollection; 
    } 
} 

Person{ 
    String name; 
    public void setName(String name) 
    { 
     this.name=name; 
    } 
    public String getName() 
    { 
     return name; 
    } 
} 

行動

SubmitItemAction 
{ 
    private Item item; 
    public getItem() 
    { 
      return item; 
    } 
    public setItem(Item item) 
    { 
      this.item=item; 
    } 
    public String submitItem() 
    { 
      dao.submit(item); 
    } 
} 

JSP

<s:text name=item.personCollection[0].name/> 
    <s:text name=item.personCollection[1].name/> 

所以這行不通。當我用上面的代碼片斷提交我的jsp時,它會給出錯誤,它無法從Item中填充personCollection。

那麼應該在jsp中命名約定。就像personCollection會成爲我可以使用的列表item.personCollection[0].someProperty一樣。但是,你如何爲類型集的集合設置名稱。

+0

你的問題是什麼? – mprabhat

+0

需要更多解釋。 –

+0

更新了問題 – kunal

回答

2

那麼在你的提交動作中使用列表,然後你可以在jsp中使用這個列表與索引。

您不能使用此設置爲集不能使用索引

在你的業務邏輯訪問列表轉換成設爲您可能需要進一步ORM設置。

+0

這看起來不錯。但我正在尋找更通用的解決方案。由於可能有多個集合正在使用POJO的實例。謝謝回覆。 – kunal

+0

那你是對的。可以嘗試使用struts的迭代器標籤。就我個人而言,我沒有這麼做,只需要幾個地方就可以這麼做。如果您獲得任何成功,請更新您的答案。 –

+0

我在jsp中使用迭代器標記。當然,如果更好的解決方案能夠解決,我會更新這篇文章。 – kunal