Saturday, 4 May 2019

Explain @ModelAttribute in Spring MVC



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.

ModelAttributeExampleController.java is controller class with @ModelAttribute method
UserDetails.java is the model object used in this example.

Finally additional definition in the spring configuration file
ModelAttributeExampleController.java

package aid.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 aid.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"/>


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

Spring Boot @ConfigurationProperties and Properties File

 In this tutorial, you will learn to use @ConfigurationProperties to map properties files to POJO classes in Spring Boot application. Let’s ...