title: Spring form - mapping list of extended classes
body: <p>I have form that contains multiple items:</p>
  
  <pre><code>&lt;form&gt;
      &lt;input name="items[0].title"&gt;
      &lt;input name="items[1].title"&gt;
      &lt;input name="items[2].title"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and classes:</p>
  
  <pre><code>class Form
  {
      private List&lt;Item&gt; items;
  
      public List&lt;Item&gt; getItems()
      {
          return items;
      }
      public void setItems(List&lt;Item&gt; items)
      {
          this.items = items;
      }
  }
  
  class Item
  {
      private String title;
  
      public String getTitle()
      {
          return title;
      }
      public void setTitle(String title)
      {
          this.title= title;
      }
  
      public String print()
      {
          System.out.println("title: " + title);
      }
  }
  </code></pre>
  
  <p>and it works fine. I can use it like this:</p>
  
  <pre><code>@RequestMapping(value="/add", method=RequestMethod.POST)
      public ModelAndView addSubmit(Form form)
  {
      form.getItems().get(1).print();
  
      ...
  }
  </code></pre>
  
  <p>What I would like to do is add new type of item. For example BigItem which has additional field size:</p>
  
  <pre><code>class BigItem
  {
      private String title;
      private String size;
  
      public String getTitle()
      {
          return title;
      }
      public void setTitle(String title)
      {
          this.title= title;
      }
      public String getSize()
      {
          return size;
      }
      public void setSize(String size)
      {
          this.size= size;
      }
  
      public String print()
      {
          System.out.println("title: " + title);
          System.out.println("size: " + size);
      }
  }
  </code></pre>
  
  <p>This is just a prototype and simplified code. I expect new quite complicated item types in future and need to be prepared for that. My idea was to create class Item with common fields and methods and extend it anytime I need new item type (BigItem for example). But i don't know how to map it. Let's assume my form looks like this:</p>
  
  <pre><code>&lt;form&gt;
      &lt;input name="items[0].title"&gt;
      &lt;input name="items[1].title"&gt;
      &lt;input name="items[1].size"&gt;
      &lt;input name="items[2].title"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and class BigItem extending Item:</p>
  
  <pre><code>class BigItem extends Item
  {
      private String size;
  
      public String getSize()
      {
          return size;
      }
      public void setSize(String size)
      {
          this.size= size;
      }
  
      public String print()
      {
          System.out.println("title: " + title);
          System.out.println("size: " + size);
      }
  }
  </code></pre>
  
  <p>How can I map it inside controller method? I would like to call method print() from BigItem class instead of Item.</p>
  
  <pre><code>form.getItems().get(1).print();
  </code></pre>