如何在 Spring MVC 的 Post/Redirect/Get 模式的请求之间传递数据?

发布于 2024-10-20 20:35:09 字数 1000 浏览 5 评论 0原文

我有一个使用 POST signin.html 提交用户名字和姓氏的表单。

 @RequestMapping(value="/signin.html",method = RequestMethod.POST)
 public ModelAndView submit(@Valid User user){
    ModelAndView mv = new ModelAndView("redirect:signin.html"); 
    //Business logic with user account
    return mv;
 } 

为了解决双重提交问题,我使用 GET 请求重定向到相同的映射。

 @RequestMapping(value="/signin.html",method = RequestMethod.GET)
 public ModelAndView submitPRG(){
     ModelAndView mv = new ModelAndView("submitted");
     mv.addObject("message", "Submitted Correctly");
     return mv;
 }

这样我就解决了双重提交问题。

我有几个问题:

1)我如何知道 /signin.html 上的 GET 请求来自重定向,并且不是用户在浏览器中请求的?我只是想关闭用户浏览 http://server/signin.html 并获得“已提交”的选项正确”的消息。我知道我可以添加类似 /signin.html?subscribed=true 的内容,但我想让它更干净。

2)有什么方法可以将 ModelAndView 对象从 submit() 传递到 submitPRG() 吗?

或者在这种情况下还有其他方法可以使用 PRG 吗?

I have a form which submits user first and last name using POST signin.html

 @RequestMapping(value="/signin.html",method = RequestMethod.POST)
 public ModelAndView submit(@Valid User user){
    ModelAndView mv = new ModelAndView("redirect:signin.html"); 
    //Business logic with user account
    return mv;
 } 

In order to solve double submit problem I'm redirecting to the same mapping using GET request.

 @RequestMapping(value="/signin.html",method = RequestMethod.GET)
 public ModelAndView submitPRG(){
     ModelAndView mv = new ModelAndView("submitted");
     mv.addObject("message", "Submitted Correctly");
     return mv;
 }

This way I solve double submit problem.

I have few questions:

1) How can I know that GET request on /signin.html coming from redirect and was not requested by user in browser? I just would like to close option for user to browse http://server/signin.html and to get "Submitted Correctly" message. I know that I can add something like /signin.html?submitted=true but I would like to make it more clean.

2) is there any way to pass ModelAndView object from submit() to submitPRG()?

Or simply is there any other way to use PRG in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北斗星光 2024-10-27 20:35:09

您想要的行为称为“闪存范围”。它通常是通过存储会话中重定向后要显示的消息来实现的。

Spring 团队承诺在 Spring 3.1 中实现它 (SPR-6464)。同时你可以在mvc-showcase 示例,非常简单,您可以自己做。

另一种方法是添加所需数据作为重定向 URL 的参数。请注意,如果您的提交方法保存模型对象,以便以后可以通过某个标识符访问它,则重定向到显示该对象的页面是有意义的(即 /users.html?userId=... 而不是 /signin.html),并在 Flash 范围内的该页面上显示“正确提交”消息。

因此,flash 作用域通常用于传递消息,而不是模型对象。

The behaviour you want is called "flash scope". It's usually implemented by storing message to be displayed after redirect in the session.

Spring team promises to implement it in Spring 3.1 (SPR-6464). Meanwhile you can take a look at the implementation of flash scope in mvc-showcase sample, it's pretty easy, so you can do it yourself.

Alternative approach is to add required data as parameters to redirect URL. Note that if your submit method saves model object so that it can be later accessed by some identifier, it makes sense to redirect to the page that displays that object (i.e. /users.html?userId=... instead of /signin.html), and display the "Submitted Correctly" message on that page from the flash scope.

So, flash scope is usually used to pass messages, not model objects.

烟花肆意 2024-10-27 20:35:09

我怎么知道 GET 请求
/signin.html 来自重定向和
浏览器中没有用户请求?

  • 您可以在会话中存储标记
  • 查看 HTTP 标头中的引用者

我建议对“正确提交”消息使用某种基于会话的消息传递。这有时称为“闪光”。 StackExchange 一直使用这些(出现在屏幕顶部的消息)。

提前谢谢你,但是我该怎么办
从 HttpServletRequest 获取引荐来源网址?

HttpServletRequest.getHeader("Referer")

How can I know that GET request on
/signin.html coming from redirect and
was not requested by user in browser?

  • You can store a marker in their session
  • Take a look at the referer in the HTTP header

I would recommend using some sort of session-based messaging for the "Submitted Correctly" message. This is sometimes called a "flash". StackExchange uses these all the time (the messages that appear at the top of the screen).

Thank you in advance, but how can I
get referrer from HttpServletRequest?

HttpServletRequest.getHeader("Referer")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文