开发者

How to update an arraylist from a jsp file using Struts2

i'm generating an html table into a jsp file using struts2. i would lik开发者_JAVA技巧e to change values contained into this arraylist, but the behaviour is not what i was expecting...

my flow: Action.java: generate an arraylist "struct" which contains "n" (for example 5) objects of type MyElem.

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

and the details of MyElem :

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

of course, all getters and setters are declared.

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>

then back in Action.java when i iterate on struct, i don't have 5 objects MyElem, but 30 : one with a "name", one with a "type", and so on for every rows... in fact i would like to have into struct one object MyElem by rows in my html table.

Thanks you !


The correct syntax to set indexed properties is

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


Try something like this in your textfield tags:

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

You should get struct as a list of MyElem, and each of the elements should have all the properties name, type, length, precision, usage and init.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜