8 Replies Latest reply: Dec 3, 2012 11:19 AM by JDBA RSS

    How to call FormHandler's handleXXX method using Ajax?

    JDBA
      Hi,

      I had a popup box which is populated when the user click on a button. I need to call the FormHandler's handleXXX method after user's form submission(In popup box) using Ajax then persist the user's details into db and display the success/failure message in same pop up box . Can we call FormHandler's handleXXX method directly using Ajax? If so, please give some information. Thanks.
        • 1. Re: How to call FormHandler's handleXXX method using Ajax?
          Gautam Singh
          You can create a hidden form (DSP:FORM) and submit is via AJAX. Most of the Javascript libraries provide mechanism to submit a form through AJAX.
          Prototype:
          http://stackoverflow.com/questions/443276/submit-a-form-via-ajax-using-prototype-and-update-a-result-div
          JQuery
          http://stackoverflow.com/questions/1960240/jquery-ajax-submit-form
          ExtJS
          http://jaspreetchahal.org/extjs-4-0-ajax-html-form-submit/
          • 2. Re: How to call FormHandler's handleXXX method using Ajax?
            karthik chopperla
            There are many ways of doing this in AJAX.
            Approach 1
            Always write your code of saving into db and other stuff into global component like a manager class. Move your code from formhandler to manager class if there is lot of logic in it

            One way is to submit form through AJAX to ( say) abc.jsp with all the parameters needed by passing in data field as below, write a droplet in abc.jsp which actually takes in all the parameters needed from the form. Droplet will call manager class and returns true/false.

            $(#formSubmitButton).click (function {
            var name = $(#name).val();
            var age =
                      $.ajax({
                      type: "POST",
                      url: "/abc.jsp",
                      data: ({name:name, age:age});
                      complete: function(request, status) {
            if(status == "success"){
            var jsonObject = request.responseText;
            if(jsonObject.success ==" true) {
            // Show jsonObject.successMessage.
            }
            }
            }
            });

            In abc.jsp
            write droplet which takes in these name, age parameters and pass it to back.

            Once you are in the output of droplet, you can create a json object {success: true/false} using JSON tag based on droplet response and return to Javascript call back function. Call back function can show the div or whatever based on the requirement.


            *Approach 2 :*
            Step1:Create Configuration.properties
            /atg/dynamo/messaging/Configuration.properties
            dispatcherServiceMap+=
            /services/servlet/ABCServlet=/your_path/DEFServlet

            Step2 : In jquery function, give Ajax post url as
            url:/services/servlet/ABCServlet

            Step 3: Create DEFServlet.properties

            $class = com.yourpath.DEFServlet.java

            Step4 DEFServlet extends DynamoServlet and in service(dynamoReq, dynamoRes){

            //Resolve formhandler by saying

            ABCFormHandler f =(ABCFormHandler)request.resolveName("/com/yourpath/formhandler");
            f. setName();
            f.setAge();
            f.handleSubmit();

            }

            Hope this helps.

            -karthik

            -karthik
            • 3. Re: How to call FormHandler's handleXXX method using Ajax?
              JDBA
              Thanks Gautam. I got some idea about Ajax from your reference link. Till I have some doubts. How could I call the FormHandler method from jsp?
              • 4. Re: How to call FormHandler's handleXXX method using Ajax?
                JDBA
                Thanks Karthik. I wrote the logic in manager class only. But I have to call formhandler's handleXXX method to do some validation. After that, I will have to call manager class to insert values in db. shall I call handleXXX method directly from droplet which is in jsp as per your Approach 2?
                • 5. Re: How to call FormHandler's handleXXX method using Ajax?
                  karthik chopperla
                  If you observe approach 1 or 2, its always droplet that is eventually called. So you can call droplet from jsp and droplet eventually invokes formhandler.

                  Make sure to build json object once droplet oparam is rendered.

                  -karthik
                  • 6. Re: How to call FormHandler's handleXXX method using Ajax?
                    Gautam Singh
                    To call the form handler method you can declare a hidden input at the end of your hidden form as:
                    <dsp:input type="hidden" bean="FormHandler.mathodName" value="Submit" priority="-10"/>

                    Also calling formhandler from droplet is not recommended as they also perform redirects and can sometimes cause issue because of race conditions.
                    formhandler are meant to be used with html forms only.
                    • 7. Re: How to call FormHandler's handleXXX method using Ajax?
                      karthik chopperla
                      Yes, that is true. Its not a good approach. We moved validations and stuff to managerial class too. Redirections from formhandler will be an issue.

                      -karthik
                      • 8. Re: How to call FormHandler's handleXXX method using Ajax?
                        JDBA
                        Thanks Gautam. It was very useful to me.