What's the best way of avoiding duplicate form submission in Spring. Does this framework provide any special feature to handle this problem (for example as tokens in Struts)?
Thanks.
What's the best way of avoiding duplicate form submission in Spring. Does this framework provide any special feature to handle this problem (for example as tokens in Struts)?
Thanks.
There are different ways to avoid double submits, which can be combined:
Use JavaScript to disable
the button a few ms after click. This will avoid multiple submits being caused by impatient users clicking multiple times on the button.
Send a redirect after submit, this is known as Post-Redirect-Get (PRG) pattern. This will avoid multiple submits being caused by users pressing F5 on the result page and ignoring the browser warning that the data will be resend, or navigating back and forth by browser back/forward buttons and ignoring the same warning.
Generate an unique token when the page is requested and put in both the session scope and as hidden field of the form. During processing, check if the token is there and then remove it immediately from the session and continue processing. If the token is not there, then block processing. This will avoid the aforementioned kinds of problems.
In Spring you can use RedirectView
as implementation of the PRG pattern (as described in point 2). The other two points needs to be implemented yourself.
Just do a redirect after post. After a form submission is successful, when returning your ModelAndView make sure the View is a RedirectView. From the user's POV, they submit the form, and then are redirected to make a "GET" to another URL. This way they won't double submit.
Note that when using a Redirect View, Model Attributes get exposed in the URL as the parameters. So you might want to keep the attributes as thin as possible. What I typically do is show the user a page that doesn't really contain any unique information, just a "confirm" message.