Friday, February 19, 2010

DWR 2.0 and Spring

Integrating DWR to project using Spring.

In this article you can find missing bit, in official DWR documentation.

1. Add dwr and dependencies to classpath, with maven it's
2. Add to web.xml Dwr Spring Servlet


3. Add to your applicationContext,xml file import (Of cause you can just put all beans inside applicationContext.xml, but I think it's more easier to have all dwr related beans in separate file).
<import resource="dwr.xml"/>

4. Add dwr.xml file with needed configuration (!!! It should be in classpath)
<?xml version="1.0" encoding="UTF-8" ?>
 <beans default-autowire="no" xmlns=""
   <dwr:convert type="bean" class="net.sf.xplanner.domain.Project">
    <dwr:exclude method="notificationReceivers"/>
    <dwr:exclude method="iterations"/>
  <dwr:controller id="dwrController" debug="true"  />
  <bean id="dwrProject" class="net.sf.xplanner.dwr.Project" >
   <property name="projectDao" ref="projectDao" />
   <dwr:remote javascript="project">
      <dwr:include method="getAllProjects" />

In dwr.xml, as you can see you should declare dwr controller.
Also we declared our dwr bean with id dwrProject, which is using "projectDao" spring bean, to use this bean you need to add following javascript to your page (we declared as accessible only one method "getAllProjects"):
<script type='text/javascript' src='/xplanner/dwr/interface/project.js'></script>
 <script type='text/javascript' src='/xplanner/dwr/engine.js'></script>

Also we declared converter for our bean, but this is very well covered by dwr documentation. We just added few exclude method to avoid of fetching huge tree of objects (Project - is hibernate bean and have a references to other objects, and by default DWR try to serialize all of them).

That's all! :-)

P.S. After small discussion we decided postpone usage of DWR in XplannerPlus project. We really miss RESTful Web Services. This feature added in version 3.0 rc1, but project look frozen more then for year in this stage.