Fancy
Fancy

Reputation: 155

Spring Redirect from POST to GET and have back button go back to previous page instead of same page

I have a Spring boot application with an html form that calls post mapping in my controller. In that post mapping, it redirects to a get mapping after it's processing. My problem with this now is when the back button of the browser is clicked, it redirects back to the same page the same number as how many times the post was called. So if I do update record 5 times in that page and then click back button , it would take 5 clicks to the back button before it proceeds back to prior page. I'm still new to this, Can anyone point me to the right direction?

Upvotes: 1

Views: 1906

Answers (2)

Ayush
Ayush

Reputation: 369

So I replicated your mentioned scenario-

So lets assume a page name contact.html

where you have a form in it now when you submit that form it will be handled by your POST MAPPING now what have you done is - return statement of your form controller has REDIRECT TO GET mapping of the SAME PAGE CONTACT.html

So let's understand the scenario -

  1. Every time you hit a submit button the request goes to POST MAPPING in the controller.

  2. Every time it redirects you to GET MAPPING and Note, this GET AND POST belong to the same page.

Now here is the trick-

  1. So EVERY TIME YOU ARE SUBMITTING you are GETTING a fresh page from the server (check every time your page reloads).

  2. As BOTH pages before making POST REQUEST and after POST REQUEST is the same file, you cannot differentiate between pages.

So as you mentioned if you update the record 5 times(let's say record1, record 2, record 3, Record 4, Record 5) then on pressing the back button you will see pages where you updated record 5, then on the next click the page where record 4 was updated and soo on...

How to visualize the change?

if you want to visualize then in POST MAPPING just redirect to another page GET REQUEST ( let say home or index).... then you press back button you will be able to see the difference as these pages are different.

Upvotes: 1

tushar
tushar

Reputation: 79

I think you have to use spring security for this.

Upvotes: 0

Related Questions