javablogspot

Just another WordPress.com 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

<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>

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=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:dwr=”http://www.directwebremoting.org/schema/spring-dwr”
xmlns:tx=”http://www.springframework.org/schema/tx”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:jee=”http://www.springframework.org/schema/jee”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd“>

Step 4: Add DWR configuration to context file

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

<dwr:configuration>
<dwr:convert type=”bean” class=”bookmarkmanager.domain.Bookmark“/>
</dwr:configuration>
<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” />
</dwr:remote>
<aop:scoped-proxy />

</bean>

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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: