Spring MVC @ModelAttribute Annotation Example
April 22, 2015
In this page we will learn how to use @ModelAttribute annotation in our spring MVC application. @ModelAttribute binds method parameter or the method response to a named model attribute. @ModelAttribute has an element as value. @ModelAttribute can be used at method level as well as parameter level. The use of @ModelAttribute at parameter level is to accept the value of form from the web request. The one use of @ModelAttribute at method level is assigning values to Model as default. This method will be created in the class annotated with @ModelAttribute. Here we will discuss both approach with example.
@ModelAttribute as Method Parameter to Access Form
When using @ModelAttribute as method parameter, it binds the form data with a bean. The controller method annotated with @RequestMapping can have custom class argument annotated with @ModelAttribute. It has one element as value which is the name of model attribute to bind. In our example we have a bean corresponding to form.Country.java
package com.concretepage.bean; public class Country { private String countryName; private String pmName; public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public String getPmName() { return pmName; } public void setPmName(String pmName) { this.pmName = pmName; } }
CountryController.java
@Controller public class CountryController { @RequestMapping(value="country", method = RequestMethod.GET) public ModelAndView country(){ return new ModelAndView("countryForm","country",new Country()); } @RequestMapping(value="saveCountry", method = RequestMethod.POST) public String saveCountry(@ModelAttribute("country") Country country, ModelMap model) { model.addAttribute("countryName", country.getCountryName()); model.addAttribute("pmName", country.getPmName()); return "success"; } }
@ModelAttribute annotated at Method Level in @ControllerAdvice Class
@ModelAttribute can also be used at method level. With @ControllerAdvice annotated class, we can add values in Model which will be known as global. It means for every request a default value will be there in our response of each and every controller method. For the demo, we are creating a class annotated with @ControllerAdvice and a method annotated with @ModelAttribute.GlobalController.java
@ControllerAdvice public final class GlobalController { @ModelAttribute public void addAttributes(Model model) { model.addAttribute("msg", "Welcome to My Country"); } }
Create JSP
For the demo, we are creating a form.countryForm.jsp
<table> <form:form action="saveCountry" method="post" commandName="country"> <tr> <td>Country Name:</td> <td><form:input path="countryName"/> </td> </tr> <tr> <td> PM Name :</td> <td><form:input path="pmName"/> </td> </tr> <tr> <td colspan=2> <input type="submit"> </td> </form:form> </table>
success.jsp
<body> <h3>${msg}</h3> <h3>Country Name: ${countryName}</h3> <h3>PM Name: ${pmName}</h3> </body>