Jul 5th, 2011, 09:59 AM
How to prevent form submits when using back (browser history)
Im using spring mvc with tiles and jsp. Normally, when users use the back button of the browser, after submitting a form, the form is submitted again.
How can i prevent this form to be submitted again (and for example show a default page showing a message like 'you cannot submit this right now')?
Thanks in advance
Jul 5th, 2011, 10:07 AM
You can use the Post-Redirect-Get design pattern.
Jul 5th, 2011, 10:11 AM
This sounds fine, but it would still not adress the issue, if the user pressed the back button..
Jul 5th, 2011, 10:27 AM
Ok i solved it now with a session variable that i set, when the form is created and removed when its submitted..
It still sometimes reacts strange but i dont get any double submits anymore..
Jul 5th, 2011, 12:51 PM
I'm not familiar with what Spring has done with the JSP layer recently, but quite a while ago Struts had implemented a mechanism that addresses this. When the page is rendered, a token value is allocated and the id for that token is rendered in a hidden field in the JSP. When the form is submitted, the action verifies that the token value equals the value submitted from the hidden field, and rejects it if it's not the same. After it makes that check, it removes the allocated token so that a resubmit after a back press will fail the verification.
Jul 6th, 2011, 07:45 AM
Post-Redirect-Get is designed/worked-out especially for this usecase... Because due to this the browser doesn't cache the post anymore and hence you can simply press back without resubmitting the form.. I suggest reading a bit more on the topic...
Originally Posted by ms-sisa
Tags for this Thread