Tuesday, November 3, 2009

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);
}
});
}

1 comment:

  1. This blog is now deprecated and please visit http://clubsking.blogspot.com/ to view this article and much more

    ReplyDelete