如何将模型属性从一个 Spring MVC 控制器传递到另一个控制器?
我正在从一个控制器重定向到另一个控制器。但我还需要将模型属性传递给第二个控制器。
我不想让模型进入会话。
请帮忙。
I am redirecting from a controller to another controller. But I also need to pass model attributes to the second controller.
I don't want to put the model in session.
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我使用 spring 3.2.3,这是我解决类似问题的方法。
1) 在控制器1的方法参数列表中添加RedirectAttributesredirectAttributes。
2) 在方法内部添加代码,将flash属性添加到redirectAttributes
redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
3) 然后,在第二个控制器使用 @ModelAttribute 注释的方法参数来访问重定向属性
以下是来自控制器 1 的示例代码:
来自控制器 2:
I use spring 3.2.3 and here is how I solved similar problem.
1) Added RedirectAttributes redirectAttributes to the method parameter list in controller 1.
2) Inside the method added code to add flash attribute to redirectAttributes
redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
3) Then, in the second contoller use method parameter annotated with @ModelAttribute to access redirect Attributes
Here is the sample code from Controller 1:
From Contoller 2:
仅使用redirectAttributes.addFlashAttribute(...) -> “redirect:...” 也有效,不必“重新插入”模型属性。
谢谢,阿博斯基!
Using just
redirectAttributes.addFlashAttribute(...) -> "redirect:..."
worked as well, didn't have to "reinsert" the model attribute.Thanks, aborskiy!
我认为最优雅的方法是在 Spring MVC 中实现自定义 Flash Scope。
闪存范围的主要思想是存储来自一个控制器的数据,直到第二个控制器中的下一次重定向
请参阅我对自定义范围问题的回答:
Spring MVC 自定义作用域 bean
此代码中唯一缺少的是以下 xml 配置:
I think that the most elegant way to do it is to implement custom Flash Scope in Spring MVC.
the main idea for the flash scope is to store data from one controller till next redirect in second controller
Please refer to my answer on the custom scope question:
Spring MVC custom scope bean
The only thing that is missing in this code is the following xml configuration:
您可以使用 org.springframework.web.servlet.mvc.support.RedirectAttributes 来解决它。
这是我的控制器示例。
在我的博客上阅读更多内容
http://mayurshah.in/596/如何重定向到页面保持模型值
You can resolve it by using org.springframework.web.servlet.mvc.support.RedirectAttributes.
Here is my controller sample.
read more on my blog at
http://mayurshah.in/596/how-do-i-redirect-to-page-keeping-model-value
我有同样的问题。
刷新页面后使用 RedirectAttributes,我的第一个控制器的模型属性已丢失。我以为这是一个错误,但后来我找到了解决方案。
在第一个控制器中,我在 ModelMap 中添加属性并执行此操作而不是“重定向”:
return "forward:/nameOfView";
这将重定向到另一个控制器,并保留第一个控制器的模型属性。
我希望这就是您正在寻找的。对不起我的英语
I had same problem.
With RedirectAttributes after refreshing page, my model attributes from first controller have been lost. I was thinking that is a bug, but then i found solution.
In first controller I add attributes in ModelMap and do this instead of "redirect":
return "forward:/nameOfView";
This will redirect to your another controller and also keep model attributes from first one.
I hope this is what are you looking for. Sorry for my English
如果您只想传递所有属性来重定向......
If you want just pass all attributes to redirect...
也许你可以这样做:
不要在第一个控制器中使用模型。将数据存储在其他共享对象中,然后第二个控制器可以检索该数据。
看看这个和这篇文章。这是关于类似的问题。
PS
您可能可以使用 session该共享数据的作用域 bean...
Maybe you could do it like this:
Don't use the model in first controller. Store data in some other shared object which could be then retrieved by second controller.
Look at this and this post. It's about the similar issue.
P.S.
You could probabbly use session scoped bean for that shared data...
我使用了
@ControllerAdvice
,请检查是否在 Spring 3.X 中可用;我在 Spring 4.0 中使用它。I used
@ControllerAdvice
, please check is available in Spring 3.X; I am using it in Spring 4.0.通过使用@ModelAttribute,我们可以将模型从一个控制器传递到另一个控制器
[输入到第一个控制器][1]
[]: https://i.sstatic.net/rZQe5.jpg
从jsp页面第一个控制器将表单数据与@ModelAttribute绑定到User Bean
By using @ModelAttribute we can pass the model from one controller to another controller
[ Input to the first Controller][1]
[]: https://i.sstatic.net/rZQe5.jpg
from jsp page first controller binds the form data with the @ModelAttribute to the User Bean
将所有模型属性作为查询字符串添加到重定向 URL。
Add all model attributes to the redirecting URL as query string.