title: Struts2 Populate Lists
body: <p>I am having issues populating a List of User defined object attributes in Struts2.</p>
  
  <p>Here is my example (getters / setters ommitted):</p>
  
  <blockquote>
    <p>public class Foo {     private String attr1;     private String attr2; 
    }  </p>
    
    <p>public class Bar {     private List foos;  } </p>
    
    <p>public class StrutsAction extends ActionSupport  {    private Bar bar;  }</p>
  </blockquote>
  
  <p>I have code in JSP as follows (extract):</p>
  
  <pre><code>&lt;tr&gt;&lt;td&gt;&lt;input type="text" name="bar.foos.attr1"/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="bar.foos.attr2"&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;input type="text" name="bar.foos.attr1"/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="bar.foos.attr2"&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>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!</p>
  
  <p>I understand I can solve the problem with hard coding the Index into the html as follows:</p>
  
  <pre><code>&lt;tr&gt;&lt;td&gt;&lt;input type="text" name="bar.foos[0].attr1"/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="bar.foos[0].attr2"&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;input type="text" name="bar.foos[1].attr1"/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="bar.foos[1].attr2"&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>I was wondering if it can be done without hard coding the Indexes?</p>