Home  >  Spring MVC

Spring MVC @ModelAttribute Annotation Example

By Arvind Rai, 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;
	}
} 
Find the Controller class. Here we have a method that has the @ModelAttribute as an argument.
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";
	}	
} 
The value of @ModelAttribute is bean name which is bound to the form. In the method we are fetching values from the form and setting it to ModelMap.

@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> 
Find the JSP file for the successful response.
success.jsp
 <body>
	<h3>${msg}</h3>
	<h3>Country Name: ${countryName}</h3>
	<h3>PM Name: ${pmName}</h3>
 </body> 

Output

To test the output, run the URL http://localhost:8080/concretepage-1/country , we will get the form.
Spring MVC @ModelAttribute Annotation Example
Fill the form and submit, we will get the response.
Spring MVC @ModelAttribute Annotation Example
What we need to observe that the first line is from global model attribute and the rest two line is from the controller method.

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us