text: Spring 3 MVC: Handling Forms in Spring 3.0 MVC
  - By Viral Patel on July 5, 2010
  -
  Our Goal
  Our goal is to create basic Contact Manager application. This app will have a form to take contact details from user. For now we will just print the details in logs. We will learn how to capture the form data in Spring 3 MVC.
  Getting Started
  Let us add the contact form to our Spring 3 MVC Hello World application. Open the index.jsp file and change it to following:
  File: WebContent/index.jsp
  The above code will just redirect the user to contacts.html page.
  The View- contact.jsp
  Create a JSP file that will display Contact form to our users.
  File: /WebContent/WEB-INF/jsp/contact.jsp
  <%@taglib uri="" prefix="form"%>
  Contact Manager
  Here in above JSP, we have displayed a form. Note that the form is getting submitted to addContact.html page.
  Adding Form and Controller in Spring 3
  We will now add the logic in Spring 3 to display the form and fetch the values from it. For that we will create two java files. First the
  Contact.java which is nothing but the form to display/retrieve data from screen and second the
  ContactController.java which is the spring controller class.
  File: net.viralpatel.spring3.form.Contact
  package net.viralpatel.spring3.form; public class Contact { private String firstname; private String lastname; private String email; private String telephone; //.. getter and setter for all above fields. }.controller; import net.viralpatel.spring3.form.Contact; import org.springframework.stereotype.Controller;.servlet.ModelAndView; @Controller @SessionAttributes public class ContactController { @RequestMapping(value = "/addContact", method = RequestMethod.POST) public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) { System.out.println("First Name:" + contact.getFirstname() + "Last Name:" + contact.getLastname()); return "redirect:contacts.html"; } @RequestMapping("/contacts") public ModelAndView showContacts() { return new ModelAndView("contact", "command", new Contact()); } }
  In above controller class, note that we have created two methods with Request Mapping /contacts and /addContact. The method
  showContacts() will be called when user request for a url contacts.html. This method will render a model with name “contact”. Note that in the
  ModelAndView object we have passed a blank Contact object with name “command”. The spring framework expects an object with name command if you are using
  in your JSP file.
  Also note that in method
  addContact() we have annotated this method with
  RequestMapping folks
  The form is completed now. Just run the application in eclipse by pression Alt + Shift + X, R. It will show the contact form. Just enter view values and press Add button. Once you press the button, it will print the firstname and lastname in sysout logs.
  Download Source Code
  Moving on
  In this article we learn how to create a form using Spring 3 MVC and display it in JSP. Also we learn how to retrieve the form values using ModelAttribute annotation. In next section we will go through form validations and different data conversion methods in Spring 3 MVC.
  Related: Spring 3 MVC Multiple Row Form Submit example
  Get our Articles via Email. Enter your email address.