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
  <dependency>
    <groupId>org.directwebremoting</groupId>
    <artifactId>dwr</artifactId>
    <version>2.0.3</version>
  </dependency>
2. Add to web.xml Dwr Spring Servlet
<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
  <init-param>
   <param-name>debug</param-name>
   <param-value>true</param-value>
  </init-param>
 </servlet>

 <servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
 </servlet-mapping>


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="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.directwebremoting.org/schema/spring-dwr
        http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
 
  <dwr:configuration>
   <dwr:convert type="bean" class="net.sf.xplanner.domain.Project">
    <dwr:exclude method="notificationReceivers"/>
    <dwr:exclude method="iterations"/>
   </dwr:convert>
  </dwr:configuration>
 
  <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" />
   </dwr:remote>
  </bean>
 
 </beans>

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.

3 comments: