Thursday, February 16, 2006

How to Stop double submit on click of refresh button in Struts

Follow these steps if you are using Struts
1. Add saveToken(request) in the execute() method of the Action when the page needs to displayed first or initialized.
2. Add the following code in the execute() method of Action handling the processing of the page on Submit

if (isTokenValid(request)) {
////// Do the processing for the request and add messages if required

resetToken(request); /// Reset the token so that on resubmit it does not come in this block
return mapping.findForward("success");
} else {
///// The page is submitted again so take app. action if required
return mapping.findForward("failure");
}