Just another weblog

Spring + DWR – Adding DWR Support to Spring Services – Part 3

Posted by damuchinni on February 13, 2009

In the last post (part 2) I covered the creation of the service layer for my AJAX/DWR/Spring application called Bookmark Management.  In this post I will cover adding DWR support the service layer, so that in my next post I can build the AJAX front-end.

Step 1: Add DWR library to project

Since the BookmarkManager-Web project is a standard Eclipse Dynamic Web Project, the DWR library is added just like any other JAVA library you want to add to a web project. If you haven’t done so already, download DWR and copy the dwr.jar file to WebContents/WEB-INF/lib.

Step 2: Add DWR servlet to web.xml


Step 3: Add DWR namespace to beans declaration

Skyway Builder generates all the spring context files to the src folder of the BookmarkManager-Web project.  Separate context files are created for each artifact category (domain, dao, service and web).  The files with the word “generated” (i.e. BookmarkManager-generated-service-context.xml) contain the configurations for the spring artifacts generated by Skyway Builder.  The files without the word “generated” (i.e. BookmarkManager-service-context.xml) contain custom Spring configurations that won’t be overwritten by the code generation engine.

To DWR-enable the Spring services you need to update the beans declaration in BookmarkManager-service-context.xml by adding the DWR namespace.

<beans xmlns=””

Step 4: Add DWR configuration to context file

Next you need to add the DWR configuration options to BookmarkManager-service-context.xml:

<dwr:convert type=”bean” class=”bookmarkmanager.domain.Bookmark“/>
<dwr:controller id=”dwrController” debug=”true” />

<bean id=”dwrBookmarkService” parent=”BookmarkService” >
<dwr:remote javascript=”bookmark“>
<dwr:include method=”deleteBookmarkOperation” />
<dwr:include method=”loadBookmarksOperation” />
<dwr:include method=”saveBookmarkOperation” />
<aop:scoped-proxy />


The dwr:configuration section is used to configure DWR.  For the most part I’m using all the default configuration options, however I am specifying a convert configuration to give DWR permission to convert the Bookmark beans for marshalling between the client (AJAX app) and server (Spring services).

In order to DWR-enable Spring services, you need to add a dwr:remote configuration to the service.  Since my services were generated, they are defined in a different context file, the context file for generated artifacts (BookmarkManager-generated-service-context.xml).  Since  changing this context file should be avoided, a slightly different solution needed.  You can add a custom bean (dwrBookMarkService) to the custom context file (BookmarkManager-service-context.xml) that extends the bean (BookmarkService) defined in generated context file.  In the custom bean can then add the remote tag which will specify which operations (method) should be proxied to the web layer.  The javascript attribute is used to specify the name of the javascript file that will be generated.

Step 5: Deploy and Verify

I can deploy the BookmarkManager-Web project to a Server (i.e. Tomcat) and verify everything is wired correctly.  DWR provides a servlet (http://localhost:8080/BookmarkManager-Web/dwr/) for testing the operations.

In the next post I will cover the development of the front-end application using AJAX.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: