”A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away” ~Antoine de Saint-Exupery

DWR / Spring MVC Quick Start

Posted: March 8th, 2009 | Author: maudrit | Filed under: Java, Quick Start | 1 Comment »

Direct Web Remoting (DWR) is a popular option for enabling Ajax support on Java based applications. This post illustrates the minimum configuration need to use DWR on a Spring MVC based application.

Configuring DWR

Add dependencies to pom.xml:
DWR has a very small foot print from the dependencies point of view. In order to use DWR you need to add the dwr.jar on your classpath. The following code snippet illustrates how this is done using Maven.


...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.directwebremoting</groupId>
<artifactId>dwr</artifactId>
<version>2.0.5</version>
</dependency>
...

Configure web.xml:
DWR offers out-of-the-box integration with Spring MVC. The following servlet must be added to the web.xml.


...
<!-- DWR Controller, handle all DWR requests -->
<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>
...

Create and configure dwr-servlet.xml:
As indicated by Spring MVC standards you must create a mapping for each Servlet. The following code provides the configuration for the dwr-servlet.xml.


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">

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

<dwr:controller id="dwrController" debug="true" />

</beans>

Create DwrFacede:
For the purpose of this sample we are illustrating how to retrieve an user using DWR. It is recommended to create a Façade to consolidate all the methods exposed using DWR, this will facilitate the management and mapping tasks.


public interface DwrFacade {

User findUser(String login);

}

Create and configure dwrContext.xml:
It is a good practice to keep all DWR definition in a separate context file. On this file two important definitions need to be provided. <dwr:configuration>  indicate how DWR will marshal any custom type (i.e. User.java); DWR is pre-configured to handle Java types likes Long, String, etc… so no need to define those here. The other important part of the configuration is the declaration of which methods are going to be available for remote calls, for example: <dwr:include method=”findUser” />.


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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="org.maudrit.quickstart.User"/>
</dwr:configuration>

<bean name="dwrFacade" class="org.maudrit.quickstart.DwrFacadeImpl">
<dwr:remote javascript="dwrFacade">
<dwr:include method="findUser" />
</dwr:remote>
<aop:scoped-proxy proxy-target-class="false" />
</bean>

</beans>

Configure applicationContext.xml:
On this sample, applicationContext.xml is the file initially loaded by Spring; since all dwr related definitions are on the dwrContext.xml, this file needs to be imported on the applicationContext.


...

<import resource="dwrContext.xml"/>

...

Executing the sample:
Open a web browser and go the following URL http://[server]:[port]/quickstart-dwr-spring-mvc

Download a complete fully functional copy of the source here.