How to pass data between requests of Post/Redirect/Get pattern in Spring MVC?
Asked Answered
P

2

13

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?

Peccant answered 8/3, 2011 at 12:59 Comment(0)
B
4

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")
Beard answered 8/3, 2011 at 15:4 Comment(1)
Thank you in advance, but how can I get referrer from HttpServletRequest?Peccant
B
17

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.

Bracket answered 8/3, 2011 at 15:2 Comment(1)
You have to be a bit careful using flash, however, as its temporal consistency is not strictly guaranteed. from the spring docs: static.springsource.org/spring/docs/3.2.x/… It likely will not be an issue, but is at least something to be aware of.Evadne
B
4

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")
Beard answered 8/3, 2011 at 15:4 Comment(1)
Thank you in advance, but how can I get referrer from HttpServletRequest?Peccant

© 2022 - 2024 — McMap. All rights reserved.