如何在 spring 引导应用程序中使用模型数据从一种控制器方法重定向到另一种方法
How to redirect from one controller method to another method with model data in spring boot application
在这个例子中,我试图从 handleSaveContact() 控制器方法从 contactSuccessMsg() 控制器方法重定向,但在传输后我需要显示成功或更新或失败消息到 UI 这只有在以下情况下才有可能我将模型数据从第一种方法传输到第二种方法。
任何人都可以建议我如何将模型数据从一种控制器方法传输到另一种控制器方法。
@GetMapping(value={"/", "/loadForm"})
public String loadContactForm(Model model) {
model.addAttribute("contact", new Contact());
return "index";
}
@PostMapping("/saveContact")
public String handleSaveContact(Contact contact, Model model) {
String msgTxt = null;
if(contact.getContactId()==null) {
msgTxt = "Contact Saved Successfully..!!";
}else {
msgTxt = "Contact Updated Successfully..!!";
}
contact.setIsActive("Y");
boolean isSaved = contactService.saveContact(contact);
if(isSaved) {
model.addAttribute("successMsg", msgTxt);
}else {
model.addAttribute("errorMsg", "Failed To Save Contact..!!");
}
return "redirect:/contactSuccessMsg";
}
/**
* To resolve Double Posting problem, redirecting the post req method to get request.
* @param contact
* @param model
* @return
*/
@GetMapping(value="/contactSuccessMsg")
public String contactSuccessMsg(Model model) {
model.addAttribute("contact", new Contact());
return "index";
}
我用的是Spring3.2.3
1.)在controller1的方法参数列表中添加RedirectAttributes redirectAttributes
public String controlMapping1(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model,
final RedirectAttributes redirectAttributes)
- 在方法中添加了将 flash 属性添加到 redirectAttributes 的代码
redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
- 然后,在第二个控制器中使用带有@ModelAttribute 注释的方法参数来访问重定向属性:
@ModelAttribute("mapping1Form") 最终对象 mapping1FormObject
这是来自控制器 1 的示例代码:
@RequestMapping(value = { "/mapping1" }, method = RequestMethod.POST)
public String controlMapping1(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model,
final RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
return "redirect:mapping2";
}
来自控制器 2:
@RequestMapping(value = "/mapping2", method = RequestMethod.GET)
public String controlMapping2(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model) {
model.addAttribute("transformationForm", mapping1FormObject);
return "new/view";
}
在这个例子中,我试图从 handleSaveContact() 控制器方法从 contactSuccessMsg() 控制器方法重定向,但在传输后我需要显示成功或更新或失败消息到 UI 这只有在以下情况下才有可能我将模型数据从第一种方法传输到第二种方法。
任何人都可以建议我如何将模型数据从一种控制器方法传输到另一种控制器方法。
@GetMapping(value={"/", "/loadForm"})
public String loadContactForm(Model model) {
model.addAttribute("contact", new Contact());
return "index";
}
@PostMapping("/saveContact")
public String handleSaveContact(Contact contact, Model model) {
String msgTxt = null;
if(contact.getContactId()==null) {
msgTxt = "Contact Saved Successfully..!!";
}else {
msgTxt = "Contact Updated Successfully..!!";
}
contact.setIsActive("Y");
boolean isSaved = contactService.saveContact(contact);
if(isSaved) {
model.addAttribute("successMsg", msgTxt);
}else {
model.addAttribute("errorMsg", "Failed To Save Contact..!!");
}
return "redirect:/contactSuccessMsg";
}
/**
* To resolve Double Posting problem, redirecting the post req method to get request.
* @param contact
* @param model
* @return
*/
@GetMapping(value="/contactSuccessMsg")
public String contactSuccessMsg(Model model) {
model.addAttribute("contact", new Contact());
return "index";
}
我用的是Spring3.2.3
1.)在controller1的方法参数列表中添加RedirectAttributes redirectAttributes
public String controlMapping1(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model,
final RedirectAttributes redirectAttributes)
- 在方法中添加了将 flash 属性添加到 redirectAttributes 的代码
redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
- 然后,在第二个控制器中使用带有@ModelAttribute 注释的方法参数来访问重定向属性:
@ModelAttribute("mapping1Form") 最终对象 mapping1FormObject
这是来自控制器 1 的示例代码:
@RequestMapping(value = { "/mapping1" }, method = RequestMethod.POST)
public String controlMapping1(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model,
final RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
return "redirect:mapping2";
}
来自控制器 2:
@RequestMapping(value = "/mapping2", method = RequestMethod.GET)
public String controlMapping2(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model) {
model.addAttribute("transformationForm", mapping1FormObject);
return "new/view";
}