@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

ModelAttributeExampleController.java

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;

@Controller
public class ModelAttributeExampleController {
	@Autowired
	private UserDetails userDetails;
	@RequestMapping(value="/modelexample")
	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
	@ModelAttribute
	public UserDetails getAccount(@RequestParam String user, @RequestParam String emailId){
		System.out.println("User Value from Request Parameter : " + user);
		userDetails.setUserName(user);
		userDetails.setEmailId(emailId);
		return userDetails;
	}
}

UserDetails.java

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;
	}
}

SpringConfiguration

<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.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Karolis says:

    your example seems to have some concurrency-issues, or why is UserDetails a singleton?

Speak Your Mind

*