2

i'm generating an html table into a jsp file using struts2. i would like 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 !

kakashi99
  • 137
  • 3
  • 11
  • please can you provide me some more details, struct is an Arraylist or bean class – developer May 10 '11 at 09:33
  • struct is an ArrayList, i've edited my post to show the creation – kakashi99 May 10 '11 at 10:03
  • instead of name="struct.length" please try by name="length" – developer May 10 '11 at 10:17
  • if i do that, my "struct" is empty. and by this way if a want to get my values i have to declare a "private String length;" in Action.java and in this variable i will have all my "length" values separeted by a "," and it is not what i want. – kakashi99 May 10 '11 at 10:25

2 Answers2

2

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>
Aravindan R
  • 3,084
  • 1
  • 28
  • 44
0

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.

TBW
  • 128
  • 1
  • 9