@ModelAttribute in Spring MVC

In this tutorial I will explain one of the most important annotation used in Spring MVC @ModelAttribute. This annotation can be used as the method arguments or before the method declaration. The primary objective of this annotation to bind the request parameters or form fields to an model object. The model object can be formed using the request parameters (as shown below in the example) or already stored in the session object. Note that, this @ModelAttribute methods are invoked before the controller methods with @RequestMapping are invoked. The logic behind the sequence is that, the model object has to be created before any processing starts inside the controller methods.

Look at the below example.

  1. ModelAttributeExampleController.java is controller class with @ModelAttribute method
  2. UserDetails.java is the model object used in this example.
  3. Finally additional definition in the spring configuration file


package javabeat.net;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

public class ModelAttributeExampleController {
	private UserDetails userDetails;
	public String getMethod(@ModelAttribute UserDetails userDetails){
		System.out.println("User Name : " + userDetails.getUserName());
		System.out.println("Email Id : " + userDetails.getEmailId());
		return "example";

	//This method is invoked before the above method
	public UserDetails getAccount(@RequestParam String user, @RequestParam String emailId){
		System.out.println("User Value from Request Parameter : " + user);
		return userDetails;


package javabeat.net;

public class UserDetails {
	private String userName;
	private String emailId;
	public String getUserName() {
		return userName;
	public void setUserName(String userName) {
		this.userName = userName;
	public String getEmailId() {
		return emailId;
	public void setEmailId(String emailId) {
		this.emailId = emailId;


<context:component-scan base-package="org.spring.examples" />
<bean id="userDetails" class="org.spring.examples.UserDetails"/>
  1. If you look at the above example, getAccount method is declared with @ModelAttribute. It means that that methods will be invoked before the controller method. That method also takes the request parameter details and setting to the object. It is one way that we are setting the values in a method.
  2. Another way is how we have used that in the controller method’s arguments. The model attribute is populated at the time of invoking the method. It is that simple to use in your project. It is powerful annotation to map your form fields to an model object.

I hope this example helps you to understand how to use @ModelAttribute for getting the request values. If you have any questions, please write it in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!