Butter Dev Logo
Search:   

February 20, 2008

DWR 2.0.x, Spring 2.x, with Spring MVC

If you are using DWR 2.0.x, Spring 2.x, with Spring MVC, dwr.xml is not required. You can use the new DWR namespace feature in your Spring xml.

This page assumes you know how to use Spring MVC, you have a Spring Dispatcher Servlet mapped (dwrSampleApp), and you are ready to integrate Spring with DWR. I strongly encourage you to have a working configuration before attempting to integrate DWR.

You can download the files from this article here. This includes all the source, configuration, and a ready to run web-app.

Step 1: Web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> 
          
  <servlet> 
    <servlet-name>dwrSampleApp</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param>
      <param-name>contextConfigLocation</param-name>
        <param-value>
          classpath:dwrSampleApp.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup> 
  </servlet> 

  <servlet-mapping> 
    <servlet-name>dwrSampleApp</servlet-name> 
    <url-pattern>*.html</url-pattern> 
  </servlet-mapping> 

  <servlet-mapping> 
    <servlet-name>dwrSampleApp</servlet-name> 
    <url-pattern>/dwr/*</url-pattern> 
  </servlet-mapping> 
</web-app>

We have added two mappings to the Dispatcher Servlet (*.html and /dwr/*). The problem with the “*.html” mapping alone is that it will not handle all of the DWR requests (described in the next section ‘Spring Application Context’). So, depending on how you have your mappings configured the /dwr/* mapping will or will not be necessary. The key thing to remember is that DWR requests need to be mapped to the configured DispatcherServlet.

Step 2: Spring xml/Application Context (for this example, dwrSampleApp.xml)

  1. Add the DWR namespace declarations to dwrSampleApp.xml:

    <beans
      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.0.xsd
        http://www.directwebremoting.org/schema/spring-dwr
        http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
    
  2. Create a DWR Controller and a way for Spring (SimpleUrlHandlerMapping) to map DWR requests to this controller. The DWR Controller needs to handle the following requests:

    • /dwr/engine.js
    • /dwr/util.js
    • /dwr/interface/**
    • /dwr/call/**

    Add the DWR Controller to dwrSampleApp.xml:

    <dwr:controller id="dwrController" debug="true" /> 
    
  3. Create the mapping from DWR requests to the DWR controller. There are several ways to create this mapping but I feel the following is the easiest to comprehend and the least verbose option:

    Add the SimpleUrlHandlerMapping to dwrSampleApp.xml:

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="alwaysUseFullPath" value="true"/>
      <property name="mappings">
      <props>
        <prop key="/dwr/**/*">dwrController</prop>
      </props>
      </property>
    </bean>
    

    Please note the use of the alwaysUseFullPath property. If this is not set to true (by default it is false) we will have to map all of the required DWR requests:

    <prop key="/interface/**">dwrController</prop>

    etc., etc.

    Why? Because we have mapped /dwr/* to the DispatcherServlet in web.xml, what gets passed into the SimpleUrlHandlerMapping is everything after the /dwr/ (We are not matching on the entire path, because alwaysUseFullPath is false by default). I think not understanding how the SimpleUrlHandlerMapping works is one of the most common problems with this configuration. This has nothing to do with DWR directly, but the DWR mailing list pays the price!

  4. Configure DWR with the configuration tag.

    For this example we are returning an Address POJO from our DWR service. We need to tell DWR to convert the Address POJO with DWR’s “bean” converter. The configuration tag’s children elements mimic the behavior of dwr.xml elements. If you are familiar with the options available in dwr.xml — setting up this tag should be second nature.

    Add the dwr:configuration tag to dwrSampleApp.xml:

      <dwr:configuration>
        <dwr:convert type="bean" class="org.uk.ltd.dwr.dev.model.Address" />
      </dwr:configuration>
    
  5. Expose your Beans to DWR with the remote tag.

    Add the following bean to dwrSampleApp.xml:

      <bean id="dwrService" class="org.uk.ltd.dwr.dev.service.DWRService">
        <dwr:remote javascript="dwrService">
          <dwr:include method="getAddress" />   
        </dwr:remote>
      </bean>
      
    

You can download the files from this article here. This includes all the source, configuration, and a ready to run web-app.

Previous Page – Next Page

7 responses to “DWR 2.0.x, Spring 2.x, with Spring MVC”

  1. Danny Garcia says:

    Thanks David, that’s what we are looking. Bram sample include dwr.xml file and we want to skip this xml file creation. In your blog you use the servlet file defintion to include the dwr configuration tags, good.

  2. MethoD says:

    I’m getting folloging issue when starting tomcat:

    The matching wildcard is strict, but no declaration can be found for element ‘dwr:controller’

  3. David says:

    MethoD you are getting this error with the download?

    If not I suggest trying the download and moving on from there. Are you sure you have the dwr.jar and the spring.jar in your WEB-INF/lib?

  4. MethoD says:

    Sure. I’m using netbeans 6.5rc2.

    It was working fine with only indexController defined.
    (I’ve zipped my project for backing up working copy)

    When I added 2 controllers I got following error:
    The matching wildcard is strict, but no declaration can be found for element ‘dwr:controller’

    Then I’ve removed the 2 controllers and I expected working fine. But I’m still getting error.

    So I’ve deleted my project and deployed the zipped one.
    Then it’s working.

    The issue is reported in the spring issue tracker and the resolution is “Cannot Reproduce”.
    jira.springframework.org/browse/IDE-865

    Christian Dupuis said:
    That basically means that the DWR team either needs to provide Eclipse support for their namespace or at the very least use the Spring tools annotation on their XSD.

  5. MethoD says:

    Can I post my project files (xml, jar, java, class)?

  6. David says:

    Batbayar,

    First of all it should be pretty easy to determine what the problem is based on the error from the server:
    No bean named ‘__dwrConfiguration’ is defined

    Secondly, I am not sure what you got out of reading my post because you surely did not follow the directions. You need to spend some time re-reading my post (STEP 4 – you obviously missed). The tag is REQUIRED. I don’t see what you are even doing with DWR with your set-up but regardless you need to at the very least add a blank dwr:configuration () to your dispatcher-servlet.xml. This is also covered in the DWR documentation: DWR docs on Spring Integration (http://directwebremoting.org/dwr/server/spring).
    -David

  7. MethoD says:

    Oh thank you! It’s working perfectly since I added following line:

    It wasn’t working at my home yesterday evening.

    And today morning I tried at school my lab and it worked.

Leave a Reply