title: How to ADD an ArrayList of bean objects using Spring Forms in Spring mvc
body: <p>I'm currently developing a simple web application using spring mvc and i'm using spring forms in my jsp pages.</p>
  
  <p>I have a grid with multiple rows, and each row is to be selected to perform basic CRUD operations (multiple rows can be selected for create scenario).
  how to persist each row (List of beans: each row is a bean object) selected using checkboxes.</p>
  
  <p>I've searched for this on plenty of forums, but couldn't get any good examples which I could use.</p>
  
  <pre><code>public class Contact {
  private String firstname;
  private String lastname;
  private String email;
  private String phone;
  
  public Contact() {
  }
  
  public Contact(String firstname, String lastname, String email, String phone) {
      this.firstname = firstname;
      this.lastname = lastname;
      this.email = email;
      this.phone = phone;
  }}
  </code></pre>
  
  <p>This is the List object</p>
  
  <pre><code>public class ContactForm {
  
  private List&lt;Contact&gt; contacts;
  
  public List&lt;Contact&gt; getContacts() {
      return contacts;
  }
  
  public void setContacts(List&lt;Contact&gt; contacts) {
      this.contacts = contacts;
  }}
  </code></pre>
  
  <p>And I tried this in my jsp page, but it dint work....</p>
  
  <pre><code>    &lt;c:forEach items="${contactForm.contacts}" var="contact" varStatus="status"&gt;
      &lt;tr&gt;
          &lt;td align="center"&gt;${status.count}&lt;/td&gt;
          &lt;td&gt;&lt;input name="contacts[${status.index}].firstname" value="${contact.firstname}"/&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input name="contacts[${status.index}].lastname" value="${contact.lastname}"/&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input name="contacts[${status.index}].email" value="${contact.email}"/&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input name="contacts[${status.index}].phone" value="${contact.phone}"/&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/c:forEach&gt;
  </code></pre>
  
  <p>controller code.</p>
  
  <pre><code>@Controller
  public class ContactController {
  
  
  private static List&lt;Contact&gt; contacts = new ArrayList&lt;Contact&gt;();
  
  static {
      contacts.add(new Contact("Barack", "Obama", "barack.o@whitehouse.com", "147-852-965"));
      contacts.add(new Contact("George", "Bush", "george.b@whitehouse.com", "785-985-652"));
      contacts.add(new Contact("Bill", "Clinton", "bill.c@whitehouse.com", "236-587-412"));
      contacts.add(new Contact("Ronald", "Reagan", "ronald.r@whitehouse.com", "369-852-452"));
  }
  
  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public ModelAndView get() {
  
      ContactForm contactForm = new ContactForm();
      contactForm.setContacts(contacts);
  
      return new ModelAndView("add_contact" , "contactForm", contactForm);
  }
  
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  public ModelAndView save(@ModelAttribute("contactForm") ContactForm contactForm) {
      System.out.println(contactForm);
      System.out.println(contactForm.getContacts());
      List&lt;Contact&gt; contacts = contactForm.getContacts();
  
      if(null != contacts &amp;&amp; contacts.size() &gt; 0) {
          ContactController.contacts = contacts;
          for (Contact contact : contacts) {
              System.out.printf("%s \t %s \n", contact.getFirstname(), contact.getLastname());
          }
      }
  
      return new ModelAndView("show_contact", "contactForm", contactForm);
  }
  }
  </code></pre>
  
  <p>The example was posted in one of the forum, I chose to use this as a template and use it according to my needs in my application...
  but in the controller, the example has static block in which the values are hardcoded, but instead I would like to add the values from the jsp page and send back the List of bean object back to the controller for persisting it in database. Please provide me suggestion on how to use the spring forms in the jsp page so that I can send back the List of bean objects back to the controller.</p>