An example of the @ModelAttribute annotation in spring mvc is introduced

  • 2020-10-23 20:57:41
  • OfStack

preface

This article introduces the very important annotation @ModelAttribute in spring mvc. This annotation can be used on method arguments or method declarations. The main purpose of this annotation is to bind the request or form parameters to the model object. Model objects can be assembled using objects stored in request or session. Note that the @ModelAttribute annotated method is executed before the controller method (@RequestMapping annotated). Because the model object is created before the controller method.

Take a look at the following example

ModelAttributeExampleController. java is the controller class, which also contains the @ModelAttribute method. UserDetails.java is the model object in this example Finally, the configuration file for spring

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd
 http://www.springframework.org/schema/jms 
 
 http://www.springframework.org/schema/jms/spring-jms-2.5.xsd">
 <context:component-scan base-package="org.spring.examples" />
 <bean id="userDetails" class="org.spring.examples.UserDetails"/>
</beans>

- In the above example, the getAccount method USES the @ModelAttribute annotation. This means that the method is executed before controller's method. This method sets the model object using the parameters of request. This is the way 1 sets a value in a method.

- Another way to use the @ModelAttribute annotation is for method parameters. The value of the model is injected when the method is called. This is very easy to use in practice. This annotation is useful when mapping form properties to model objects.

conclusion


Related articles: