You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Wangenheim, Marc" <MW...@GAIC.COM> on 2002/09/19 21:01:34 UTC

Need help with multiple submit and ActionForward

I need to prevent users from trying to submit a form multiple times
while still forwarding to a jsp after the form has been saved
successfully. Basically the user clicks on a javascript menu that calls
a javascript save function (see below). The save function sets the
struts action and submits the form. Once the form has been successfully
saved the action class forwards the user to another jsp. I have tried to
intercept all form submission but the first using a javascript global
variable. This works and prevents the user from submitting the form more
than once but unfortunatly the ActionForward never triggers leaving the
user stuck on the first jsp. Is this something I will have to handle on
the server side using tokens ?? Any help much appreciated.

-Marc

>From the jsp:
<html:form action="SHSMI_Save.do" enctype="multipart/form-data">
// save: Submits the form back to struts
function save(){
var aform = document.forms[0];
if (aform.attachment.value>"") {
  alert("You must click the 'Add Attachment' button to attach the file
before saving the document");
  aform.attachment.focus();
}
else {
aform.action="SHSMI_Save.do";
aform.submit();
}
}

And here's part of the struts action class:
public ActionForward perform(ActionMapping aMapping,
                             ActionForm aForm,
                             HttpServletRequest aRequest,
                             HttpServletResponse aResponse)
                     throws ServletException {
  String action = aMapping.getPath();
  if (!action.equals("/Login") && !action.equals("/LoginVerify")) {
    if (aRequest.getSession().getAttribute("edHID")==null) {
      LoginBean relogin = new LoginBean();
      relogin.setError("Your session timed out. Please
re-authenticate.");
      aRequest.getSession().setAttribute("Relogin",relogin);
      ActionForward fw = new ActionForward("/Login.jsp?");
      fw.setRedirect(true);
      return fw;
    }
  }
  String hid=Security.getHid(aRequest.getSession());
  Statement statement=null;
  DB database = ServletTools.createDB(getServlet().getServletContext(),
SHSMI_Const.DATABASE);
  Connection connection=database.getConnection();
  if (connection == null)
    return error(aMapping,aRequest,database,statement,"Unable to connect
to the database");
  statement=database.getStatement(connection);
  if (action.equals("/Login"))
    return
login(aMapping,aForm,aRequest,aResponse,database,statement,hid);
  if (action.equals("/LoginVerify"))
    return
loginVerify(aMapping,aForm,aRequest,aResponse,database,statement,hid);
  if (action.equals("/SHSMI_Delete_Temp_Attachment"))
    return
shsmiDeleteTempAttachment(aMapping,aForm,aRequest,aResponse,database,sta
tement,hid);
  if (action.equals("/SHSMI_Delete_Attachment"))
    return
shsmiDeleteAttachment(aMapping,aForm,aRequest,aResponse,database,stateme
nt,hid);
  if (action.equals("/SHSMI_Save_Attachment"))
    return
shsmiSaveAttachment(aMapping,aForm,aRequest,aResponse,database,statement
,hid);
  if (action.equals("/SHSMI_New"))
    return
shsmiNew(aMapping,aForm,aRequest,aResponse,database,statement,hid);
  if (action.equals("/SHSMI_Edit"))
    return
shsmiEdit(aMapping,aForm,aRequest,aResponse,database,statement,hid);
  if (action.equals("/SHSMI_Delete"))
    return
shsmiDelete(aMapping,aForm,aRequest,aResponse,database,statement,hid);
  if (action.equals("/SHSMI_Save"))
    return
shsmiSave(aMapping,aForm,aRequest,aResponse,database,statement,hid);
  if (action.equals("/SHSMI_Display"))
    return
shsmiDisplay(aMapping,aForm,aRequest,aResponse,database,statement,hid);
  if (action.equals("/SHSMI_Send"))
    return
shsmiSend(aMapping,aForm,aRequest,aResponse,database,statement,hid);
  if (action.equals("/SHSMI_Print_Main"))
    return
shsmiRenderMainPrintForm(aMapping,aForm,aRequest,aResponse,database,stat
ement,hid);
  if (action.equals("/SHSMI_Print_Referral"))
    return
shsmiRenderReferralPrintForm(aMapping,aForm,aRequest,aResponse,database,
statement,hid);
  System.out.println("Invalid ACTION: "+action);
  return error(aMapping,aRequest,database,statement,"Invalid ACTION");
}
} //SHSMI_Action
private ActionForward shsmiSave(final ActionMapping aMapping,
                                          final ActionForm aForm,
                                          final HttpServletRequest
aRequest,
                                          final HttpServletResponse
aResponse,
                                          final DB aDatabase,
                                          final Statement aStatement,
                                          final String aHid) {
  Table_SHSMI f = (Table_SHSMI) aForm;
  int id=StringEx.stringToInt(f.getId());
  boolean newRecord=(StringEx.isEmpty(f.getLast_Saved()));
  String
err=ActionTools.editCheck(f.getLast_Saved(),SHSMI_Const.APP_NAME,Table_S
HSMI.NAME,id,aRequest,aDatabase,aStatement);
  if (err!=null) return
error(aMapping,aRequest,aDatabase,aStatement,err);
  try {
//this is where it saves to the DB
  catch (Exception ex) {
    return error(aMapping,aRequest,aDatabase,aStatement,"Unable to
process additional tables");
  }
  f.destroy();
  return success(aMapping,aRequest,aDatabase,aStatement);
This is the ActionForward:
private ActionForward success(ActionMapping aMapping,
                              HttpServletRequest aRequest,
                              DB aDatabase,
                              Statement aStatement) {
 ActionForward fw = aMapping.findForward("success");
 fw.setRedirect(true);
 aDatabase.releaseAll(aStatement);
 return fw;
}

Re: Need help with multiple submit and ActionForward

Posted by Danny Mui <da...@muibros.com>.
What I've seen done before in the past is when a user clicks submit, 
disable the submit button until the response is ready.

Tokens won't really help since the output of the first request will not 
be returned back to the browser since the second takes precendence as in 
your original scenario.  Tokens will make sure only one form gets 
processed though.

Of course I could be wrong!

danny

Wangenheim, Marc wrote:

>I need to prevent users from trying to submit a form multiple times
>while still forwarding to a jsp after the form has been saved
>successfully. Basically the user clicks on a javascript menu that calls
>a javascript save function (see below). The save function sets the
>struts action and submits the form. Once the form has been successfully
>saved the action class forwards the user to another jsp. I have tried to
>intercept all form submission but the first using a javascript global
>variable. This works and prevents the user from submitting the form more
>than once but unfortunatly the ActionForward never triggers leaving the
>user stuck on the first jsp. Is this something I will have to handle on
>the server side using tokens ?? Any help much appreciated.
>
>-Marc
>
>>>From the jsp:
><html:form action="SHSMI_Save.do" enctype="multipart/form-data">
>// save: Submits the form back to struts
>function save(){
>var aform = document.forms[0];
>if (aform.attachment.value>"") {
>  alert("You must click the 'Add Attachment' button to attach the file
>before saving the document");
>  aform.attachment.focus();
>}
>else {
>aform.action="SHSMI_Save.do";
>aform.submit();
>}
>}
>
>And here's part of the struts action class:
>public ActionForward perform(ActionMapping aMapping,
>                             ActionForm aForm,
>                             HttpServletRequest aRequest,
>                             HttpServletResponse aResponse)
>                     throws ServletException {
>  String action = aMapping.getPath();
>  if (!action.equals("/Login") && !action.equals("/LoginVerify")) {
>    if (aRequest.getSession().getAttribute("edHID")==null) {
>      LoginBean relogin = new LoginBean();
>      relogin.setError("Your session timed out. Please
>re-authenticate.");
>      aRequest.getSession().setAttribute("Relogin",relogin);
>      ActionForward fw = new ActionForward("/Login.jsp?");
>      fw.setRedirect(true);
>      return fw;
>    }
>  }
>  String hid=Security.getHid(aRequest.getSession());
>  Statement statement=null;
>  DB database = ServletTools.createDB(getServlet().getServletContext(),
>SHSMI_Const.DATABASE);
>  Connection connection=database.getConnection();
>  if (connection == null)
>    return error(aMapping,aRequest,database,statement,"Unable to connect
>to the database");
>  statement=database.getStatement(connection);
>  if (action.equals("/Login"))
>    return
>login(aMapping,aForm,aRequest,aResponse,database,statement,hid);
>  if (action.equals("/LoginVerify"))
>    return
>loginVerify(aMapping,aForm,aRequest,aResponse,database,statement,hid);
>  if (action.equals("/SHSMI_Delete_Temp_Attachment"))
>    return
>shsmiDeleteTempAttachment(aMapping,aForm,aRequest,aResponse,database,sta
>tement,hid);
>  if (action.equals("/SHSMI_Delete_Attachment"))
>    return
>shsmiDeleteAttachment(aMapping,aForm,aRequest,aResponse,database,stateme
>nt,hid);
>  if (action.equals("/SHSMI_Save_Attachment"))
>    return
>shsmiSaveAttachment(aMapping,aForm,aRequest,aResponse,database,statement
>,hid);
>  if (action.equals("/SHSMI_New"))
>    return
>shsmiNew(aMapping,aForm,aRequest,aResponse,database,statement,hid);
>  if (action.equals("/SHSMI_Edit"))
>    return
>shsmiEdit(aMapping,aForm,aRequest,aResponse,database,statement,hid);
>  if (action.equals("/SHSMI_Delete"))
>    return
>shsmiDelete(aMapping,aForm,aRequest,aResponse,database,statement,hid);
>  if (action.equals("/SHSMI_Save"))
>    return
>shsmiSave(aMapping,aForm,aRequest,aResponse,database,statement,hid);
>  if (action.equals("/SHSMI_Display"))
>    return
>shsmiDisplay(aMapping,aForm,aRequest,aResponse,database,statement,hid);
>  if (action.equals("/SHSMI_Send"))
>    return
>shsmiSend(aMapping,aForm,aRequest,aResponse,database,statement,hid);
>  if (action.equals("/SHSMI_Print_Main"))
>    return
>shsmiRenderMainPrintForm(aMapping,aForm,aRequest,aResponse,database,stat
>ement,hid);
>  if (action.equals("/SHSMI_Print_Referral"))
>    return
>shsmiRenderReferralPrintForm(aMapping,aForm,aRequest,aResponse,database,
>statement,hid);
>  System.out.println("Invalid ACTION: "+action);
>  return error(aMapping,aRequest,database,statement,"Invalid ACTION");
>}
>} //SHSMI_Action
>private ActionForward shsmiSave(final ActionMapping aMapping,
>                                          final ActionForm aForm,
>                                          final HttpServletRequest
>aRequest,
>                                          final HttpServletResponse
>aResponse,
>                                          final DB aDatabase,
>                                          final Statement aStatement,
>                                          final String aHid) {
>  Table_SHSMI f = (Table_SHSMI) aForm;
>  int id=StringEx.stringToInt(f.getId());
>  boolean newRecord=(StringEx.isEmpty(f.getLast_Saved()));
>  String
>err=ActionTools.editCheck(f.getLast_Saved(),SHSMI_Const.APP_NAME,Table_S
>HSMI.NAME,id,aRequest,aDatabase,aStatement);
>  if (err!=null) return
>error(aMapping,aRequest,aDatabase,aStatement,err);
>  try {
>//this is where it saves to the DB
>  catch (Exception ex) {
>    return error(aMapping,aRequest,aDatabase,aStatement,"Unable to
>process additional tables");
>  }
>  f.destroy();
>  return success(aMapping,aRequest,aDatabase,aStatement);
>This is the ActionForward:
>private ActionForward success(ActionMapping aMapping,
>                              HttpServletRequest aRequest,
>                              DB aDatabase,
>                              Statement aStatement) {
> ActionForward fw = aMapping.findForward("success");
> fw.setRedirect(true);
> aDatabase.releaseAll(aStatement);
> return fw;
>}
>
>  
>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>