This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
What is the best way of handling struts forms with multiple submit buttons. For a long time, to give the user the opportunity to change their minds, I've been building forms along the lines of
and this has always worked fine. Until today when I needed a quick way to prevent double submits on a particular from. So I added an onsubmit handler to the form to disable the "Continue" button after the first click. That part worked, but the action="processForm" never got called. When I looked at the POSTs in Firebug with and without the onsubmit handler I saw;
The action parameters have been dropped from the submission. In this case, Struts simply re-displayed the form to me.
Have I been doing things the wrong way all this time and how do I modify the form to get out of this particular problem?
My answer does not address your issue directly but I think you can give it a look. Its quick and easy and does not require any coding .
How to Prevent Duplicate Form Submission Using Struts 2 Facilities.
The purpose of this document is to solely illustrate how Struts 2 handles double submission. Even though I have used some actions related to ‘users’ please don’t compare this with actual requirements of APPS.
Duplicate form submission can occur as a result any of the following actions performed by a user:
1. Using the Back button of the browser to submit the same form.
2. Refreshing the confirmation page of a successful form submission.
[Form to add user(FORM1)]---------------->[updateUserAction]------------------------>[Confirm page that user has been successfully added(FORM2)]
Step 1: User Submits FORM 1 to add user.
Step 2: User is presented with FORM 2 which tells the user that a user has been added successfully.
Case 1: From FORM 2 user come back to FORM 1 by using browser BACK button and resubmits FORM 1. This constitutes double submission of a form.
Case 2: User refreshes FORM 2 which may constitutes double submistion of a form
How to prevent Double Submission
1. Within the <form> tags in FORM 1 and FORM 2 insert the following struts tag:
For ease of mainainence and identifiability insert the <token> tag right after the <form> tag.
2. In struts.xml add the following to updateUser.action(highlighted in yellow)
3. In case of double submission struts returns a result named “invalid.token” as highlighted in yellow above. This result type can be handled like any other result type.
4. In case of double submission struts adds an action error to the request. This can be used in any desired way by using the <s:actionerror/> tag.
The changes for this example have been checked in. The example can be viewable after a new build has been generated and deployed.
Prevention of double submission has been in implemented in the “Update User” use case. I have created a common tile definition (dblSbmt) for the invalid.token. The same tile definition can be used by all (the usefulness of his aproach is moot.) . Alternativly double submission can be handled differently in diferent usecases.
For duplicate form submission ::
Struts2 has token and tokenSession interceptor which prevents the duplicate form submission. You have to use either of the interceptor in the action along with the <s:token/> tag.