我正在寻找一种解决方案,将2个@ModelAttributes从我的GET控制器传递到我的POST控制器而不使用Session。我正在使用Spring Boot和Thymeleaf。 我的观点仅限于其中一个,但第二个控制器需要同时使用它们。问题是第二个控制器重新初始化视图中未使用的@ModelAttribute。
控制器:
//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.
}
观点:
<form action="simulations2" th:action="@{/simulations2}" th:object="${sim_class}" method="post">
…
</form>