Skip navigation




Avoid Form Data Duplicate submitting


Discussion
Form data duplicate submitting can caused by either:
1.Using the browser BACK button to return to the previous page and resubmit the form.2.Refreshing this page and selecting OK when the broswer ask whether you want to resubmit the form data.

Solution

Using Token.

The following is the sample code:

public class InitAction extends Action { 
  public ActionForward execute( 
    ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 
    GkyForm gkyForm = (GkyForm) form; 
    gkyForm.setKey(""); 
    gkyForm.setValue(""); 
    // avoid duplicated submitting to set Token here. 
    saveToken(request); 
    return mapping.findForward("gkyInitOk"); 
  } 
}  

public class SubmitAction extends Action { 
  public ActionForward execute( 
    ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 
    ActionErrors errors = new ActionErrors(); 
    if(!isTokenValid(request)) { 
      errors.add(ActionMessages.GLOBAL_MESSAGE, 
        new ActionMessage("errors.token")); 
    } 
    resetToken(request); 
    if(!errors.isEmpty()) { 
      saveErrors(request, errors); 
      saveToken(request); 
      return mapping.getInputForward();  
    } 
    GkyForm gkyForm = (GkyForm) form; 
    String key = gkyForm.getKey(); 
    String value = gkyForm.getValue(); 
    // ..... 
    return mapping.findForward("doAddOk"); 
  } 
}

The following is Processing Flow

processing-flow.gif

Leave a comment