我有/
的控制器和另一个用于提交表单的控制器,我希望在表单提交后重定向到/
控制器,并显示已成功注册的消息。
这是/
的控制器:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String mainPage(@RequestParam(value = "registerSuccessful", required = false) boolean registerSuccessful,
ModelMap modelMap) throws UnknownHostException {
boolean isDeviceRegistered = mainService.isDeviceRegistered();
modelMap.addAttribute("isDeviceRegistered", isDeviceRegistered);
modelMap.addAttribute("registerSuccessful", registerSuccessful);
return "main";
}
和/registerForm
的控制器:
@RequestMapping(value = "registerForm", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
public String saveDeviceInfo(@ModelAttribute("deviceInfo") DeviceInfo deviceInfo,
RedirectAttributes redirectAttributes,
BindingResult result, ModelMap modelMap){
mainService.saveDeviceInfo(deviceInfo);
redirectAttributes.addFlashAttribute("registerSuccessful", true);
return "redirect:/";
}
如您所见,我发送了true
属性registerSuccessful
但是当我在重定向后阅读registerSuccessful
处理程序上/
的内容时,它是错误的。
我该如何解决?
答案 0 :(得分:0)
像这样使用@ModelAttribute
:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String mainPage(@ModelAttribute(value = "registerSuccessful") boolean registerSuccessful,
ModelMap modelMap) throws UnknownHostException {
boolean isDeviceRegistered = mainService.isDeviceRegistered();
modelMap.addAttribute("isDeviceRegistered", isDeviceRegistered);
modelMap.addAttribute("registerSuccessful", registerSuccessful);
return "main";
}