Saturday, February 9, 2013

Call a Struts 2 action using a javascript and load the response to a div

A common problem that Struts 2 frame work users face is how to call a Struts 2 action using a java script and load the response to a div, specially when you are not using Struts dojo tags. So by using the following function you can achieve this easily. 

Note: You should include this function at the top most parent page.

dojo.require("dojo.io.IframeIO");
dojo.require("dojo.dom");

function sendToTarget(submitUrl , formId , divId, parameter1, parameter2){
          dojo.require("dojo.io.IframeIO");
          dojo.io.bind({
                    url:submitUrl,
                    formNode: dojo.byId(formId),
                    mimetype: "text/html",
                    transport: "IframeTransport",
                    preventCache: true,
                    multipart: true,

                    content:{
                              urlParameter1: parameter1,
                              urlParameter2: parameter2
                    },

                    load: function(type, data, evt) {
                              var divDisplay = dojo.byId(divId);
                              data = data.body.innerHTML;
                              divDisplay.innerHTML = data;
                    }, 

                    error: function(type, errObj){
                              alert("error " + errObj.message);
                    }
          });
}

No comments:

Post a Comment