I am having issues populating a List of User defined object attributes in Struts2.
Here is my example (getters / setters ommitted):
public class Foo { private String attr1; private String attr2; }
public class Bar { private List foos; }
public class StrutsAction extends ActionSupport { private Bar bar; }
I have code in JSP as follows (extract):
<tr><td><input type="text" name="bar.foos.attr1"/></td><td><input type="text" name="bar.foos.attr2"</td></tr>
<tr><td><input type="text" name="bar.foos.attr1"/></td><td><input type="text" name="bar.foos.attr2"</td></tr>
I need each table row to create 1 foo item in the List with each attribute, however my code creats a new foo object for each attribute when passed through and I end up with 4 foos rather than 2!
I understand I can solve the problem with hard coding the Index into the html as follows:
<tr><td><input type="text" name="bar.foos[0].attr1"/></td><td><input type="text" name="bar.foos[0].attr2"</td></tr>
<tr><td><input type="text" name="bar.foos[1].attr1"/></td><td><input type="text" name="bar.foos[1].attr2"</td></tr>
I was wondering if it can be done without hard coding the Indexes?
I think you can use the Struts 2 iterator tag:
<s:iterator var="foo" value="bar.foos">
<tr><td><s:property value="attr1"/> .....
</s:iterator>
If it does not work, we can think in other solution.
You can find more information here: http://struts.apache.org/2.3.1/docs/iterator.html
[]s