1

Goal

Basically, I want to distinguish between a normal page submission and a Refresh event fired on Browser window. So that I can stop re-submission.

Brief

I have Spring MVC App that has Rest URLs tagged to each Controller, say, one Spring Controller for each CRUD operation. Now, on submitting a request to Add/Edit request a POST request is sent to the backend controller for the respective Id.

For example
localhost.xxx/add --> add request is sent for a new id auto-generated from backend.
localhost.xxx/edit --> edit request is sent for the id\ record currently edited. 

Issue

In my case On pressing F5 after submission again the old request is sent to the backed for the same id.

Achieve

I's like to stop this re-submission of form when F5 key is pressed somehow. Any workaround frontend or backend (redirect event) will do, however, I would be glad to know/prefer what is the ideal way to stop such re-submissions.

yeppe
  • 679
  • 1
  • 11
  • 43

1 Answers1

2

You could use Post-Get-Redirect.

Once the POST is done ,instead of returning a view name send a redirect request using "redirect:<pageurl>".

And and have a method with method = RequestMethod.GET there return the view name.

So the post method will give a redirect response to the browser then the browser will fetch the redirect url using get method since resubmission is avoided.

Reference avoid-form-resubmission-when-refreshing-the-page

Alien
  • 15,141
  • 6
  • 37
  • 57
  • the approach is very simple that you should redirect once form get submitted and you should have some url mapped to redirected url so that you can return page from that. – Alien Nov 12 '18 at 09:58
  • Q: How would one preserve some information , for instance, save error message value in model object's field value while redirecting? how would that be handled, I want to maintain model attribute value, across redirected pages (I want to maintain it in request scope)? – yeppe Nov 13 '18 at 06:19
  • Well, I figured this out. I used redirectAttributes --> Add Flash Attributes to achieve this. Thanks anyways for the research! – yeppe Nov 16 '18 at 08:45