My Struts2 action looks as follows:
@ParentPackage("json-default")
@Namespace("/testing")
public class AjaxTestAction
{
@Action( interceptorRefs={@InterceptorRef(value="json", params={"enableSMD","true"})},
results={@Result(name = "success", type="json", params={"enableSMD","true"})} )
public String smd() {
return "success";
}
@SMDMethod
public String sayHello(String name)
{
return ("Hello " + name);
}
}
My javascript looks as follows:
//load dojo RPC
dojo.require("dojo.rpc.JsonService");
//create service object(proxy) using SMD (generated by the json result)
var service = new dojo.rpc.JsonService("<%=contextPath%>/testing/ajax-test.action");
// execute remote service call
var deferred = service.sayHello('Amit');
deferred.addCallback(function(result) {
alert(result.toString());
});
That's all you need. Cool eh?? One thing to note above is the url to the action - I'm using the convention plugin - but yours could look different. Have fun with Dojo. To me AJAX has never been easier. I'm not a UI guy, but my application is really looking like a RIA with Dojo toolkit and this JSON-RPC communication with the backend Java Struts2 actions.
- Amit
No comments:
Post a Comment