Java 如何从另一个传递表单对象以及调用的操作方法调用弹簧控制器@RequestMapping
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22703501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call spring controller @RequestMapping from another action method passing form object along with the call
提问by romil gaurav
I need to call another @RequestMapping from inside of a spring controller. Now I can easily do it by
我需要从弹簧控制器内部调用另一个 @RequestMapping 。现在我可以轻松地通过
return "redirect:anotherMapping.htm";
But the I need to pass value in form object as well.
但是我也需要在表单对象中传递值。
@RequestMapping("/anotherMapping")
public ModelAndView addUser(final @ModelAttribute("userLogin") UserLogin
userLogin, final HttpServletRequest request){
I need to pass userId in UserLogin. If I write return "redirect:anotherMapping.htm";, then it invokes this controller method but form object is null.
我需要在 UserLogin 中传递 userId。如果我写 return "redirect:anotherMapping.htm";,那么它会调用这个控制器方法,但表单对象为空。
Please help.
请帮忙。
采纳答案by gerrytan
You can use RedirectAttributes.
您可以使用重定向属性。
In below example, when foo is invoked and redirects to /bar
, the model will contain xyz=meow
attribute.
在下面的示例中,当 foo 被调用并重定向到 时/bar
,模型将包含xyz=meow
属性。
@RequestMapping(..)
public String foo(RedirectAttributes redir) {
redir.addFlashAttribute("xyz", "meow");
return "redirect:/bar";
}
@RequestMapping("/bar")
public String bar(Model model) {
..
}
If you find doing this too many time you might also consider using @SessionAttributes
如果您发现这样做的时间太多,您也可以考虑使用@SessionAttributes