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.

lunes, 27 de julio de 2009

Using DWR with Springframework (I)

Recently, I have integrated DWR (direct web remoting) in a spring application. There are some ways to do that, I'm going to explain only one, that uses spring support for namespaces introduced since Spring 2.0.

The first step is adding a new servlet in web.xml and map it to /dwr/*:


Second step is adding the namespace to our applicationContext:



This allows us to use DWR namespace for spring. Now, we are able to add the DWR controller to manage DWR requests:



We have setup dwr with spring, it's as easy as it seems. Supposing we have a service that returns a java.util.List of Article class. Article could be a simple bean like this:



To setup getLastArticles() as a remote ajax method with Spring we only have to annotate the related ArticleService spring bean:



That's all. We are telling DWR to convert our bean Article to JSON. And, only the method specified in dwr:include tag, is callable by ajax. We can call this method from any view in our web application. In the next episode, I will explain how.

Using DWR with Springframework (II)

Deploying your own jar in OC4J 10.1.3

I recently had a big problem working with Oracle Application Server 10.1.3 at work. We are developing a web application based on spring and hibernate. The application was only an Eclipse project, and I thought that it was a mistake because we were mixing in a single project business layer, controller, view, and data acces. So, I thought splitting the project in some little ones, would be a great deal:
  • Model: a project containing domain code, in our case, hibernate entities.
  • DAO: Data acces objects.
  • DTO: Data transfer objects.
  • Util: util classes.
  • Service: Business code.
  • Web: Web app, controllers and configuration files (application context, hibernate maps, etc...)
To do this, I created a modular project with maven. But when I tried to deploy to oc4j I got a ClassNotFoundException from Spring application context. I didn't understood why, because my jars where present in my app lib folder. So, I went to OC4J console to test if my jars were loaded by the classloader. Them were not present. Why isn't oc4j loading my jars? Third party libraries, like spring and hibernate, were loaded, so I asked myself: Which is the difference between that ones and my own jars? So I opened a spring jar, and I found that it had a complete manifest.mf file. I thought this could be the problem, and reading oc4j documentation, I confirmed that. We have to create a MANIFEST.MF for each jar. So I created a manifest file in src/main/META-INF/MANIFEST.MF in each project home:


Extension-Name: com.yourcompany.yourpackage
Specification-Title:
Specification-Version:
Specification-Vendor:
Implementation-Version:
Implementation-Vendor-Id: com.yourcompany.yourpackage
Implementation-Vendor:
Implementation-Title:


Obviously, you have to fill-in the file with the appropiate values. The second step is including our manifest.mf file in maven's build:



Now, the next time you run mvn package your manifest.mf will be included in your jar and oc4j classloader will load your own jars.