Butter Dev Logo

February 20, 2008

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

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

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" 
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> 





Since we are not using Spring MVC there are two things that we need to take care of in the web.xml:

  1. We need a way for our application to find and load our Spring configuration. There is more than one way to do this but I recommend using a Spring ContextLoadListener.
  2. The DwrSpringServlet needs to be specified and all DWR requests need to be mapped to it.

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

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

  2. 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 dwrSampleApp2.xml:

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

    Add the following bean to dwrSampleApp2.xml:

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

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

Leave a Reply