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.