Reputation: 1442
Just got a strange behavior with validation of a very simple HTML form with Spring MVC and Thymleaf.
I have this methods for edit page rendering and submitted form handle.
@GetMapping("/{id}")
public String editPage(@PathVariable("id") Long id, Model model) {
model.addAttribute("user", userService.findById(id)
.orElseThrow(NotFoundException::new));
return "user_form";
}
@PostMapping("/update")
public String update(@Valid UserRepr user, BindingResult result, Model model) {
logger.info("Update endpoint requested");
if (result.hasErrors()) {
return "user_form";
}
userService.save(user);
return "redirect:/user";
}
The wired thing in the update() method is that in case of a validation error the attribute of the model with form content has the name "userRepr" but not "user" as I expect and the thymleaf form view failed because of that.
It's easy to fix the problem by renaming the attribute but Is there some contract about such attribute naming? Is it changeable?
Upvotes: 0
Views: 95
Reputation: 3945
You can do this using @ModelAttribute
.
@ModelAttribute is used to map/bind a a method parameter or method return type to a named model attribute. See @ModelAttributes JavaDoc. This is a Spring annotation.
@PostMapping("/update")
public String update(@Valid @ModelAttribute("user") UserRepr user, BindingResult result, Model model) {
logger.info("Update endpoint requested");
if (result.hasErrors()) {
return "user_form";
}
userService.save(user);
return "redirect:/user";
}
Model object auto generates attribute names and then forward the above method calls to addAttribute(Object attributeValue)
.
Following are the rules of name generation strategy:
- For an object which is not a collection, short class name is generated. For example for java.lang.String, 'string' will be generated.
- For collections/arrays, 'List' is appended after the type of elements in it e.g. 'stringList'. The collection/array should not be empty because the logic uses the first element to find it's type.
You can check this link for more details. https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/spring-model-attribute-generated-names.html
Upvotes: 1