Search⌘ K
AI Features

Making Updates Persistent

Explore methods to make user edits persistent in your Thymeleaf and Spring Boot application. Learn how to refactor the TeamController and TeamService using parameter classes to ensure data updates are saved effectively, enhancing maintainability and the interaction between the web and domain layers.

Updating team controller

We can’t add or remove rows yet, but we can edit the values of each player via the rows that are rendered by Thymeleaf on the server. To make those edits persistent, we need to update TeamController.doEditTeam(). The controller currently looks like this:

Java
@PostMapping("/{id}")
@Secured("ROLE_ADMIN")
public String doEditTeam(@PathVariable("id") TeamId teamId,
@Valid @ModelAttribute("team") EditTeamFormData formData,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("editMode", EditMode.UPDATE);
model.addAttribute("users", userService.getAllUsersNameAndId());
model.addAttribute("positions", PlayerPosition.values());
return "teams/edit";
}
service.editTeam(teamId, formData.getVersion(), formData.getName(), formData.getCoachId());
return "redirect:/teams";
}

We could expand ...