I'm looking for a solution to pass 2 @ModelAttributes from my GET controller to my POST controller without using Session. I'm using Spring Boot and Thymeleaf. My view is bound only to one of those but the second controller need to use them both. The problem is that the second controller reinitialize the @ModelAttribute not used in view.
The controller:
//in this controller I've initialized the first @modelAttribute
@RequestMapping(value="/simulations", method=RequestMethod.POST)
public String checkSimulationsManagerForm(@Valid @ModelAttribute("sim_manager") Simulations_manager sim_manager, BindingResult bindingResult, final RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
return "set_simulations-manager_form";
}
redirectAttributes.addFlashAttribute("sim_manager", sim_manager);
return "redirect:/simulations2";
}
//GET
@RequestMapping(value="/simulations2", method=RequestMethod.GET)
public String showSimulationsClassForm(@ModelAttribute("sim_class") Simulations_class sim_class, @ModelAttribute("sim_manager") Simulations_manager sim_manager){
//Do some stuff
return "set_simulations-class_form";
}
//POST that reinitilize the firse @ModelAttribute
@RequestMapping(value="/simulations2", method=RequestMethod.POST)
public String checkSimulationsClassForm( @Valid @ModelAttribute("sim_class") Simulations_class sim_class, BindingResult bindingResult,@ModelAttribute("sim_manager") Simulations_manager manager,final RedirectAttributes redirectAttributes) {
//ERROR IS HERE, manager attributes are all null, even if they were not.
}
The view:
<form action="simulations2" th:action="@{/simulations2}" th:object="${sim_class}" method="post">
…
</form>