lunes, 17 de agosto de 2009

Using DWR with Springframework (II)

In this chapter, I will explain how to call our dwr remote service from the client. But before, let me tell you a problem with Spring Framework and DWR integration.

Is very usual using aop for transactions with spring. In that case, if our dwr-allowed service is proxied the remote call will fail. To avoid this we only need to add this section of code in the service bean definition:

Now, the DWR call will arrive to the proxied bean. Don't forget that because is a very common issue.

Now, let's go with the dwr client side explanation as I promise in the previous post. The first step is including dwr's javascript files in our jsp file:

This loads DWR base code. Note that /dwr was the url-pattern from the DWR servlet. Engine.js and util.js are created on runtime (these files are inside dwr jar). And... where is located our service? Very easy:

Note that DWR uses the name of the java class as the js filename and for the javascript object. Now, to call our service method:

Very easy, don't you think? Once we can call our service method, we need a callback function to get the data. So let's create one:

DWRUtil.addRows creates all the rows of our table and fills the cells with the received data. Look up the third parameter of addRows. Is an array of functions that tells DWR wich data we want to be showed on each column. In that case, data is a JSON representation of our Article bean. We are telling here to DWR that we want to show id property in the first column, title in the second column and bodytext in the third one.

That's all. I've use for this example a method without parameters. To use with some parameters, is the same procedure, you only have to put callback as last parameter. Suppose this new method from our service class:

So now, to call this service:


That's all. I will publish in the future a third article, when DWR 3 is launched. This version will add support to use annotations in the service instead of xml setup in the application context bean definition file.