ModelAndView
@ModelAttribute
ModelMap
Command Object
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC Form Submission</title> </head> <body> <form:form method="POST" action="addCustomer"> <table> <tr> <td>First Name:</td> <td><form:input path="firstName" /></td> </tr> <tr> <td>Last Name:</td> <td><form:input path="lastName" /></td> </tr> <tr> <td>Address:</td> <td><form:input path="address" /></td> </tr> <tr> <td>Contact:</td> <td><form:input path="contact" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </form:form> </body> </html>
Note 1:
We have used <%@taglib uri=”http://www.springframework.org/tags/form” prefix=”form”%> which comes from Spring MVC
Note 2: See the form code. We have used path.For example <form:input path=”firstName” />. Its just setFirstName in the command object. We will discuss about the command object in just few minutes
Model Class:
package com.proliferay.demo.model; public class Customer { private String firstName; private String lastName; private String address; private String contact; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getContact() { return contact; } public void setContact(String contact) { this.contact = contact; } }
Note : We have created the above POJO for capturing the form data. We will use this as command object for the form. Command object is an ordinary object which holds the form data.
The Controller:
package com.proliferay.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.proliferay.demo.model.Customer; @Controller public class FormController { @RequestMapping("/") public ModelAndView showHomePage() { return new ModelAndView("home", "command", new Customer()); } @RequestMapping(value = "/addCustomer", method = RequestMethod.POST) public String addCustomer(@ModelAttribute Customer customer, ModelMap model) { model.addAttribute("firstName", customer.getFirstName()); model.addAttribute("lastName", customer.getLastName()); model.addAttribute("address", customer.getAddress()); model.addAttribute("contact", customer.getContact()); return "result"; } }
Note 1: How the form knows where to store form data? See the code for showHomePage method. In that method we have create ModelAndView object with command as attribute name and Customer.java as command object.
return new ModelAndView(“home”, “command”, new Customer());
We can interpret the above line something like this
home.jsp is the view page
Create object of Customer.java
Use the Customer object as command object
So when we display a form with command object as above, Spring MVC will automatically figure it out that form data should be stored in the command object. The command object is simply contains setter and getter methods. If we write in the form <form:input path=”address” /> then spring will call setAddress() of the command object and address will be stored in the object.
Note 2: Use of @ModelAttribute
When we submit the form it calls addCustomer method where @ModelAttribute is used. It simply retrieve the command object of the submitted form. In our case the command object is nothing but new Customer().
Note 3: Use of ModelMap
Before displaying the result page we can prepare ModelMap object so that we can retrieve information from the ModelMap in the result page. For example model.addAttribute(“firstName”, customer.getFirstName()); So in the result page we can use ${firstName} to retrieve First Name.
The result page:
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC Form Handling</title> </head> <body> <h2>Customer Information</h2> <table> <tr> <td>First Name:</td> <td>${firstName}</td> </tr> <tr> <td>Last Name:</td> <td>${lastName}</td> </tr> <tr> <td>Address:</td> <td>${address}</td> </tr> <tr> <td>Contact:</td> <td>${contact}</td> </tr> <tr> <td colspan="2"><a href="${pageContext.request.contextPath}">Back</a></td> </tr> </table> </body> </html>
Summery: The above example can be summarized as
- Before showing the first page i.e., the form, create the command object in the controller.
- Display the form with the command object.
- Fill the form. Form data will be binned to the command object.
- Submit the form.
- After submission of the form retrieve the command object in the Controller using @ModelAttribute to read form data.
- Before displaying the final result page prepare ModelMap in the controller.
- Display the result page.
- Use the ModelMap to display data in the result page.