ATG formhandler AJAX call using Jquery, JSON

Story:

  1. Click on Anchor tag
  2. Anchor tag invokes javascript function using attribute onClick()
  3. Javascript function submits a jsp form using ajax, which calls formhandler
  4. This Javascript function also define success method
  5. formhandler sets some parameter values.
  6. Control is redirected to success page by form handler.
  7. Success JSP creates JSON object.
  8. Javascript success function recieves this JSON object as response.
  9. Javascript parse this JSON object as required.

 

JSP Anchor tag:
On click on this link, formhandler method should be invoked and should return some result.

<a href="#" onClick="invokeTestMethod();">Test</a>

Javascript function:

function invokeTestMethod(){
 
//define success method
 var options = {
 success : resultOfTestMethod
 }
 
 //submit form using ajax
 $('#<form ID>').ajaxSubmit(options); 
}

JSP Form:

In JSP i have created one form which will be submitted by JQurey on click on the link:

 <dsp:form id="<form name>" name="<form name>" method="post" action="${requestURI}">
              <dsp:input type="hidden" bean="MyFormHandler.updateAddressSuccessUrl" value="<Success JSP>"/>
              <dsp:input type="hidden" bean="MyFormHandler.updateAddressErrorURL" value="<Error JSP>"/>
              <dsp:input type="hidden" bean="MyFormHandler.testMethod" value=""/> 
 </dsp:form>

FormHandler method:

public final boolean handleTestMethod(final DynamoHttpServletRequest pRequest,
 final DynamoHttpServletResponse pResponse) throws ServletException,
 IOException {
setTestString("Mohut");
 return checkFormRedirect(updateAddressSuccessUrl,updateAddressErrorURL, pRequest,pResponse);
 }

Success JSP:

<dsp:page>
<dsp:importbean bean="/path/MyFormHandler"/>
 <json:array name="addressList">
 <json:object>
 <json:property name="<propert name>">
 <dsp:valueof bean="MyFormHandler.testString"/>
 </json:property>
 </json:object>
 </json:array>
</dsp:page>

Javascript success method:

function resultOfTestMethod(responseText, statusText, xhr, $form){
var addressArray = JSON.parse(responseText);
}