簡體   English   中英

如何從另一個與表單一起傳遞表單對象的動作方法中調用spring控制器@RequestMapping

[英]how to call spring controller @RequestMapping from another action method passing form object along with the call

我需要從彈簧控制器內部調用另一個@RequestMapping。 現在我可以輕松地做到這一點

    return "redirect:anotherMapping.htm";

但是我也需要在表單對象中傳遞值。

    @RequestMapping("/anotherMapping")
    public ModelAndView addUser(final @ModelAttribute("userLogin") UserLogin 
    userLogin, final HttpServletRequest request){

我需要在UserLogin中傳遞userId。 如果我寫返回“ redirect:anotherMapping.htm”;那么它將調用此控制器方法,但form對象為null。

請幫忙。

您可以使用RedirectAttributes。

在下面的示例中,當foo被調用並重定向到/bar ,模型將包含xyz=meow屬性。

@RequestMapping(..)
public String foo(RedirectAttributes redir) {
  redir.addFlashAttribute("xyz", "meow");
  return "redirect:/bar";
}

@RequestMapping("/bar")
public String bar(Model model) {
  ..
}

如果您發現執行此操作的時間過多,則還可以考慮使用@SessionAttributes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM