Butter Dev Logo
Search:   

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" 
  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"> 
   
   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

   <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>
       classpath:dwrSampleApp2.xml
     </param-value>
   </context-param>

   <servlet>
     <servlet-name>dwr</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</servlet-name>
     <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping> 

</web-app>

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:

    <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. 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:configuration>
        <dwr:convert type="bean" class="org.uk.ltd.dwr.dev.model.Address" />
      </dwr:configuration>
    
  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" />   
        </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.

Leave a Reply

Optimization WordPress Plugins & Solutions by W3 EDGE