2011-05-10 75 views
2

我使用struts2生成了一個html表格轉換爲jsp文件。我想改變這個數組列表中的值,但行爲不是我期待的...如何使用Struts2從jsp文件更新數組列表

我的流程: Action.java:生成一個包含「n」的數組列表(例如5 )MyElem類型的對象。

private ArrayList<MyElem> struct; 
public void setStruct(...) {...} 
public ArrayList<MyElem> getStruct() {...} 

和MyElem的細節:

private String name; 
private String type; 
private int length; 
private int precision; 
private String usage; 
private String init; 
當然

,所有的getter和setter聲明。

test.jsp的:

<s:iterator value="struct" status="elemsStatus"> 
<tr> 
<td><s:textfield name="struct.name" value="%{name}" theme="simple"/></td> 
    <td><s:textfield name="struct.type" value="%{type}" theme="simple"/></td> 
    <td><s:textfield name="struct.length" value="%{length}" theme="simple"/></td> 
    <td><s:textfield name="struct.precision" value="%{precision}" theme="simple"/></td> 
    <td><s:textfield name="struct.usage" value="%{usage}" theme="simple"/></td> 
    <td><s:textfield name="struct.init" value="%{init}" theme="simple"/></td> 
    </tr> 
</s:iterator> 

然後回到Action.java當我反覆結構,我沒有5個對象MyElem,但30:一個用「名」,一個帶有「類型「,等等每行... 其實我想要在我的HTML表格中的行到結構一個對象MyElem。

謝謝你!

+0

請你能給我提供一些更多的細節,結構是一個ArrayList或者bean類 – developer 2011-05-10 09:33:42

+0

結構是一個ArrayList,我已經編輯我的職務,以顯示創建 – kakashi99 2011-05-10 10:03:45

+0

而不是name =「struct.length」,請嘗試按名稱=「長度」 – developer 2011-05-10 10:17:31

回答

2

正確的語法來設置索引屬性是

<s:iterator value="struct" status="elemsStatus"> 
<tr> 
<td><s:textfield name="struct[%{#elemsStatus.index}].name" value="%{name}" theme="simple"/></td> 
    <td><s:textfield name="struct[%{#elemsStatus.index}].type" value="%{type}" theme="simple"/></td> 
    <td><s:textfield name="struct[%{#elemsStatus.index}].length" value="%{length}" theme="simple"/></td> 
    <td><s:textfield name="struct[%{#elemsStatus.index}].precision" value="%{precision}" theme="simple"/></td> 
    <td><s:textfield name="struct[%{#elemsStatus.index}].usage" value="%{usage}" theme="simple"/></td> 
    <td><s:textfield name="struct[%{#elemsStatus.index}].init" value="%{init}" theme="simple"/></td> 
    </tr> 
</s:iterator> 
+0

這非常有幫助。謝謝! – Tiago 2012-05-08 10:39:54

0

嘗試這樣的事情在你的文本字段標籤:

<s:textfield name="struct[#elemsStatus.index].yourProperty" value="yourValue" theme="simple" /> 

你應該得到結構作爲MyElem的列表,和每一個元素都應該有所有屬性名稱,類型,長度,精度,用法init

相關問題