title: Fill a list from JSP in Spring
body: <p>I have something like this in my Spring Application:</p>
  
  <pre><code>public class Book{ 
      public Book(){
         sheets = new LinkedList&lt;Sheet&gt;();
      }
      protected List&lt;Sheet&gt; sheets;
      //getter and setter 
  }
  </code></pre>
  
  <p>I add several Sheets to the sheet list and I print a form in a JSP like this:</p>
  
  <pre><code>&lt;form:form modelAttribute="book"  action="${dest_url}" method="POST"&gt;   
      &lt;c:forEach items="${mybook.sheets}" var="sheet" varStatus="status"&gt;
          &lt;form:hidden path="sheet[${status.count -1}].header"/&gt;
          &lt;form:hidden path="sheet[${status.count -1}].footer"/&gt;
          &lt;form:hidden path="sheet[${status.count -1}].operador"/&gt;
          &lt;form:hidden path="sheet[${status.count -1}].number"/&gt;
          &lt;form:hidden path="sheet[${status.count -1}].lines"/&gt;
      &lt;/c:forEach&gt;
      ...
  &lt;/form:form&gt;
  </code></pre>
  
  <p>I need to get back this list in the controller when the form is submitted. So in my controller I have a method with a parameter like this:</p>
  
  <pre><code>public String myMethod (@ModelAttribute("book") Book book, Model model){
      ...
  }
  </code></pre>
  
  <p>The problem is that it doesn't fill the sheets list unless in the constructor of Book I add as much Sheet's as I want to get. The problem is that I don't know in advance the number of Sheets the book is going to have. </p>
  
  <p>I think the problem is that in my method it instantiates Book which has a list of sheets with 0 elements. When it tries to access to sheets[0] the list is empty and it doen't add a Sheet. I've tried to create a getter method for the list with an index parameter (so it can create the element if it doesn't exists in the list like in Struts framework) like this one:</p>
  
  <pre><code>public Sheet getSheets(int index){
      if(sheets.size() &lt;= index){
          Sheet sheet = new Sheet();
          sheets.add(index, sheet);
      }
      Sheet sheetToReturn = sheets.get(index);
      if(sheetToReturn == null){
          sheetToReturn = new Sheet();
          sheets.add(index, sheetToReturn);
      }
      return sheetToReturn;
  }
  </code></pre>
  
  <p>but with this method the JSP doesn't work because sheets has an invalid getter.</p>
  
  <p>What's the proper way of filling a list when you don't know the number of items in advanced?</p>
  
  <p>Thanks</p>