202014Oct

checkFormRedirect In ATG

checkFormRedirect() In ATG is : –
  • method of GenericFormhandler class
  • use to redirect to the SuccessURL if no form errors are found and to ErrorURL if form errors are found.
  • also use to forward the request rather than redirect (based on userForwards parameter)
  • internally use redirectOrForward method os same class.
  • return true or false
Use of checkFormredirect()
public boolean handleMethod(..,..)
{
   //
   // Logic
   //
   .
   .
   .
   return checkFormRedirect(successURL,errorURL,request,response);
}
Forward Request Instead of Redirect

There are some situations when there is need to forward the request to get request parameter on success url. So to acheive this functionality we just need to set useForwards property true.

<dsp:form ...>
.
.
.
<dsp:input type="hidden" bean="YourFormHandler.useForwards" value="true">
.
.
.
</dsp:form>
Internal Implementation of chechkFormRedirect()
public boolean checkFormRedirect(String pSuccessURL,
                                   String pFailureURL,
                                   DynamoHttpServletRequest pRequest,
                                   DynamoHttpServletResponse pResponse)
    throws ServletException,
           IOException {
    if (getCheckForValidSession() && !isValidSession(pRequest)) {
      addFormException(new DropletException("Your session expired since this form was displayed - please try again.", "sessionExpired"));
    }

    //If form errors were found:
    if (getFormError()) {

      //If FailureURL was not passed in, stay on same page, return true:
      if (StringUtils.isBlank(pFailureURL)) {
        if (isLoggingDebug()) {
          logDebug("error - staying on same page.");
        }
        return true;
      }

      //If FailureURL was passed in, redirect and return false:
      else {
        if (isLoggingDebug()) {
          logDebug("error - redirecting to: " + pFailureURL);
        }
        redirectOrForward(pRequest, pResponse, pFailureURL);
        return false;
      }
    }

    //If no form errors were found:
    else {

      //If SuccessURL was not passed in, stay on same page, return true:
      if (StringUtils.isBlank(pSuccessURL)) {
        if (isLoggingDebug()) {
          logDebug("no form errors - staying on same page.");
        }
        return true;
      }

      //If SuccessURL was passed in, redirect and return false:
      else {
        if (isLoggingDebug()) {
          logDebug("no form errors - redirecting to: " + pSuccessURL);
        }
        redirectOrForward(pRequest, pResponse, pSuccessURL);
        return false;
      }
    }
  }
Internal Implementation of redirectOrForward()
  protected void redirectOrForward(DynamoHttpServletRequest pRequest,
      DynamoHttpServletResponse pResponse,
      String pURL) throws IOException, ServletException {
    boolean bUseForwards = ServletUtil.isForwardRequest(pRequest, isUseForwards()); 
    if (bUseForwards) {
      ServletUtil.forwardRequest(pRequest, pResponse, pURL);
    } else {
      pResponse.sendLocalRedirect(pURL, pRequest);
    }
  }

Note :- checkormRedirect() is provided by GenericFormhandler class so you need to extend this class to use this method.