What is the purpose and usage of @ModelAttribute
in Spring MVC?
At the Method Level
1.When the annotation is used at the method level it indicates the purpose of that method is to add one or more model attributes
@ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("india", "india");
}
At the Method Argument 1. When used as a method argument, it indicates the argument should be retrieved from the model. When not present and should be first instantiated and then added to the model and once present in the model, the arguments fields should be populated from all request parameters that have matching names So, it binds the form data with a bean.
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(@ModelAttribute("employee") Employee employee) {
return "employeeView";
}