Wednesday, February 18, 2009

Struts2 Annotations, Dojo and Json plugin

Here's a nice way to integrate Dojo, json with the latest Struts2.1 web framework. A point to note here is that Dojo support via a plugin is deprecated in Struts2.1. My example does not use the dojo plugin, but the dojo library directly with the help of the json-plugin from the Google repository.

My Struts2 action looks as follows:

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";

public String sayHello(String name)
return ("Hello " + name);

My javascript looks as follows:

//load dojo RPC

//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) {

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: