wood burning stoves 2.0*
The moose likes Struts and the fly likes Struts 2: Forms with multiple submit buttons - Best Practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2: Forms with multiple submit buttons - Best Practice?" Watch "Struts 2: Forms with multiple submit buttons - Best Practice?" New topic
Author

Struts 2: Forms with multiple submit buttons - Best Practice?

Glen Divers
Ranch Hand

Joined: Jan 21, 2010
Posts: 61
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?

Regards
Pallav Bora
Ranch Hand

Joined: Oct 13, 2009
Posts: 73
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)]








Normal Operation:
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.
Problem Operation:
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:

<s:token/>

Example :

<s:form action=”xyz”>
<s:token/>

</s:form>

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)

<action name=" updateUser" method="updateUser" >
<interceptor-ref name="token"/>
<interceptor-ref name="defaultStack"/>
<result type="tiles" name="invalid.token">dblSbmt</result>


<result type="tiles" name="success">updtUsr</result>

</action>

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.
An Example
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.







Nidhi Garg
Greenhorn

Joined: Jun 19, 2009
Posts: 21
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2: Forms with multiple submit buttons - Best Practice?