2012-04-20 61 views
4

我目前有以下代碼,數據顯示正常。Struts邏輯:迭代輸入字段

<logic:iterate name="myList" id="product" indexId="iteration" type="com.mycompany.MyBean"> 
    <tr> 
     <td> <bean:write name="product" property="weight"/> </td> 
     <td> <bean:write name="product" property="sku"/> </td> 
     <td> <bean:write name="product" property="quantity"/> </td> 
    </tr> 
</logic:iterate> 

但現在我需要使「數量」部分可修改。用戶應該能夠更新該字段,按提交,當它發送到服務器時,「myList」應該自動更新新的數量。

我試過尋找這方面的幫助,但我一直髮現的僅僅是如何顯示數據,而不是修改它的例子。任何幫助,將不勝感激。

+0

您不能使用邏輯標籤直接更新。您可以使用ajax並在文本字段的onchange事件中觸發ajax函數,以更新服務器中的數量。 – 2012-04-20 12:58:54

+0

哇...這將是一個很長的回答... – Th0rndike 2012-04-20 12:59:32

+0

@RavindraGullapalli這是不正確的,我已經做到了。 – Th0rndike 2012-04-20 13:00:25

回答

4

所以這很棘手,因爲有很多事情要完成才能使其工作。首先,使用html標籤在迭代器中聲明你的標籤,屬性爲INDEXED = TRUE,ID爲不同於該名稱的ID,我還取出了「indexId」屬性以使用簡單的索引字索引:

<logic:iterate name="myList" id="myListI" type="com.mycompany.MyBean"> 
<tr> 
    <td> <html:input name="myListI" property="weight" indexed="true"/> </td> 
    <td> <html:input name="myListI" property="sku" indexed="true"/> </td> 
    <td> <html:input name="myListI" property="quantity" indexed="true"/> </td> 
</tr> 

後,爲了使支柱能夠獲取和設置bean的屬性,你需要聲明EXTRA get和您的收藏對象內部設置方法,使用你的ID寫名字的迭代標籤。在這種情況下,你會寫2種額外的get和set方法爲「myListI」:

public void setMyListI(int index, myBean value){ 
    this.myList.add(value); 
} 
public myBean getMyListI(int index){ 
    return this.myList.get(index); 
} 
+0

嗨,只有數量應該是可以修改的,其餘的應該保持只讀。我應該使用什麼標籤? – LatinCanuck 2012-04-20 15:47:27

+0

只是玩它而已。使用bean:爲不應該修改的標記或已禁用的html:輸入。有很多方法可以實現這一點,這取決於你如何實現它。 – Th0rndike 2012-04-23 07:46:18

+0

優秀的答案。在我的情況下,我需要getNameOfId(int index)來使解決方案正常工作,但我單獨發佈該解決方案以供參考。謝謝! – javatestcase 2012-10-07 15:52:21

1

理論上,indexed attribute of the struts html tags可以用於此:

僅對內的logic:iterate標籤。如果爲true,則html標籤的名稱將呈現爲「id [34] .propertyName」。括號中的數字將爲每次迭代生成,並從祖先邏輯:迭代標記中獲取。

但是,html:errors標籤上沒有相應的indexed屬性,這限制了它的實用性。而且,id,nameproperty屬性的所需組合可能相當混亂。

我發現使用jsp scriptlet生成包含迭代索引的屬性名更容易。以下代碼要求您的表單具有字符串數組屬性「quantity」。

<% int idx=0; %> 
<logic:iterate ...> 
    <html:text property='<%= "quantity[" + idx + "]" %>'/> 
    <html:errors property='<%= "quantity[" + idx + "]" %>'/> 
    <% i++; %> 
</logic:iterate> 
2

我覺得Th0rndikes答案多半是正確的。我的實現稍有不同,所以它也值得嘗試。

形式

private List<Parameter> activeParameters; 

public List<Parameter> getActiveParameters() { 
    return activeParameters; 
} 

public Parameter getParam(int index){ 
    return this.activeParameters.get(index); 
} 

JSP

<logic:iterate name="MyForm" property="activeParameters" id="param"> 
    <tr> 
    <td><bean:write name="param" property="prompt"/></td> 
    <td><html:text name="param" property="value" indexed="true"/></td> 
    </tr> 
</logic:iterate> 

總之,我並沒有在iterate標籤使用類型,使用屬性標籤來代替。在這個bean中添加一個getter,並在JSP(param)中將索引的迭代ID的名稱與方法參數進行匹配。

0

在此請看:http://wiki.apache.org/struts/StrutsCatalogLazyList

索引屬性

Struts的html標籤有一個索引的屬性,這將產生 相應的HTML來填充豆的集合,當表單提交 。 訣竅是將id屬性命名爲與 索引屬性相同。

例如下面的JSP ...

<logic:iterate name="skillsForm" property="skills" id="skills"> 

     <html:text name="skills" property="skillId" indexed="true"/> 

    </logic:iterate> 

...將生成以下HTML

<input type="text" name="skills[0].skillId value="..."/> 
<input type="text" name="skills[1].skillId value="..."/> 
.... 
<input type="text" name="skills[n].skillId value="..."/> 

當表單提交的BeanUtils將第一調用 getSkills(index)方法來檢索索引bean f在檢索到的bean上執行 setSkillId(..)。