Skip to content

Spring Framework Blog

Menu
  • Home
  • Spring
  • Spring Boot
  • About
Menu

Spring MVC Entities

Posted on June 3, 2024June 7, 2024 by Armando Marques

Table of Contents

Toggle
  • Annotations
  • @ModelAttribute
  • BindingResult

Annotations

NameDescription
@ModelInterface, holder for model attributes
@ModelMapClass, in ModelMap attibute key can be omitted and the value of the attribute will be use to generate the key.
@ModelAndViewModelAndView it’s a container for both View object and ModelMap

@ModelAttribute

Annotation that binds a method parameter or method return method to a named model attribute.

Method level

The purpose of the method is to add one or more model attributes. This methods can’t be directly mapped to requests. Spring MVC will always make a call to this method before any request handler controllers.

@ModelAttribute
public void addAttributes(Model model) {
    model.addAttribute("msg", "Welcome to the Netherlands!");
}

Method argument

The argument will be retrieved from the model

@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(@ModelAttribute("employee") Employee employee) {
    // Code that uses the employee object

    return "employeeView";
}

https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation

BindingResult

[BindingResult] is Spring’s object that holds the result of the validation and binding and contains errors that may have occurred. The BindingResult must come right after the model object that is validated or else Spring will fail to validate the object and throw an exception.

When Spring sees @Valid, it tries to find the validator for the object being validated. Spring automatically picks up validation annotations if you have “annotation-driven” enabled. Spring then invokes the validator and puts any errors in the BindingResult and adds the BindingResult to the view model.

 @PostMapping("/register")
public String doPost(AuthUserDetails user, BindingResult bindingResult) {
if (securityService.userExists(user.getUsername())) {
return "user/userExists";
}
securityService.addUser(user);
return "redirect:/login";
}

References

https://www.baeldung.com/spring-mvc-model-model-map-model-view

https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation

https://stackoverflow.com/questions/1848

  • Spring
  • Documentation
  • References
  • Toc
  • Books
  • Certification
  • AOP
  • Config
  • Java
  • Java core
  • JDBC
  • JPA
  • Rest
  • Security
  • Spring
  • Spring Boot
  • Spring Core
  • Spring Data
  • Spring MVC
  • Spring Rest
  • Spring Security
  • Tests
  • Transactions
  • Uncategorized

Recent Posts

  • Spring Annotations
  • Java Tests
  • Java operators
  • Java versions
  • Java Oracle Licenses
  • Configuration properties
  • MockMvc
  • Spring Security III
  • MVC Controller Method Params
  • JPA Methods
  • Transaction propagation and isolation
  • Spring JDBC
  • Spring Boot Auto-Configuration
  • Spring Resource interface
  • JSR 330 Standard Annotations
  • Spring Aware Interfaces
  • Spring Transactions
  • Spring Boot Core
  • MVC Rest
  • Spring Boot JPA
©2025 Spring Framework Blog | Built using WordPress and Responsive Blogily theme by Superb