Spring configuration of DWR.
I have found there really aren’t any great guides for getting DWR configured with spring. Here is my experiences.
Prerequisites:
- Assuming some previous knowledge of DWR and spring.
- Spring is configured and working.
- DWR has been added the WEB-INF/lib.
1. Add DWR to the web.xml
Instead of using the DwrServlet, make sure you use the SpringDwrServlet:
<servlet> <servlet-name>dwr-invoker</servlet-name> <display-name>DWR Servlet</display-name> <description>Direct Web Remoter Servlet</description> <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class> <init-param> <param-name>logLevel</param-name> <param-value>INFO</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet></tt> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping><
2. Next, add the dwr namespace and schema to your spring configuration file:
The namespace is xmlns:dwr=”http://www.directwebremoting.org/schema/spring-dwr”
The schemaLocation is http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd" default-autowire="no" default-lazy-init="false" default-init-method="init" default-destroy-method="destroy"></tt> </beans>
3. Configure Spring creators.
There are two options for doing this. You can directly expose spring beans:
<bean id="dwrWatchesBean" class="com.jivesoftware.community.action.WatchesAction" > <dwr:remote javascript="Watches" /> <property name="communityManagerImpl" ref="communityManagerImpl" /> <property name="forumManagerImpl" ref="forumManagerImpl" /> <property name="watchManagerImpl" ref="watchManagerImpl" /> <property name="documentManagerImpl" ref="documentManagerImpl" /> <property name="blogManagerImpl" ref="blogManagerImpl" /> <property name="userManagerImpl" ref="userManagerImpl" /> </bean>
The dwr:remote element is the element that will expose the bean to dwr. In my opinion this is the best approach as it allows you to wire you dependencies like any other spring object.
Secondly you can configure them in a dwr:configuration much in the same as the standard dwr.xml file works:
<dwr:configuration> <dwr:create type="new" javascript="Watches" class="com.jivesoftware.community.action.WatchesAction" /> </dwr:configuration>
4. Configure spring convertors
The spring configurators must be configured inside of a dwr:configuration element in spring.
<dwr:configuration> <dwr:convert type="bean" class="com.jivesoftware.community.Watch" /> </dwr:configuration>
5. Other important elements
dwr:include : Used to include specific methods. Can be used with dwr:convert, dwr:create, or dwr:remote element
dwr:exclude : Used to exclude specific methods. Can be used with dwr:convert, dwr:create, or dwr:remote element
dwr:init : Optional element that will allow you to specify custom creator types and converter types.
dwr:converter : Specifies a custom converter type, must be used within dwr:init.
dwr:create : Specifies a custom creator type, must be used within a dwr:init.
dwr:signature : Used to specify signatures much like the like element in the standard dwr.xml.
6. Full Example
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd" default-autowire="no" default-lazy-init="false" default-init-method="init" default-destroy-method="destroy"></tt> <bean id="dwrWatchesBean" class="com.jivesoftware.community.action.WatchesAction" > <dwr:remote javascript="Watches" /> <property name="communityManagerImpl" ref="communityManagerImpl" /> <property name="forumManagerImpl" ref="forumManagerImpl" /> <property name="watchManagerImpl" ref="watchManagerImpl" /> <property name="documentManagerImpl" ref="documentManagerImpl" /> <property name="blogManagerImpl" ref="blogManagerImpl" /> <property name="userManagerImpl" ref="userManagerImpl" /> </bean> <dwr:configuration> <dwr:convert class="com.jivesoftware.community.DraftImpl" type="bean" /> <dwr:create type="new" javascript="ImagePicker" class="com.jivesoftware.community.action.ImagePicker" > <dwr:include method="getImageByName" /> <dwr:include method="getImageDetailsByName" /> <dwr:include method="getImageDetailsByURL" /> <dwr:include method="removeImage" /> </dwr:create> <dwr:convert class="com.jivesoftware.base.User" type="bean" > <dwr:include method="username"/> <dwr:include method="name"/> <dwr:include method="ID"/> </dwr:convert> <dwr:convert class="com.jivesoftware.community.action.LDAPGroupBean" type="bean" /> <dwr:signatures> <![CDATA[ import java.util.Map; import com.jivesoftware.community.DraftImpl; DraftImpl.setProperties(Map<String, String> properties); DraftImpl.setImageIDs(List<Long> imageIDs); ]]> </dwr:signatures> </dwr:configuration> </beans>
CAVEATS
- Do not attempt to use DwrSpringServlet with org.springframework.web.servlet.mvc.ServletWrappingController. DwrSpringServlet expects the spring context to be fully initialized before it attempts to initialize.
Thanks so much for posting this, i was pulling out my hair trying to get spring working with DWR. Turns out I wasn’t using the SpringDwrServlet — duh!
Alex
February 11, 2009 at 2:49 pm
If you are using spring mvc, you do not need to use SpringDWRServlet, the normal spring servlet can be used instead, just make sure you have the following in your spring config:
<dwr:controller id="dwrController" debug="true"/> <dwr:configuration/> <bean id="beanUrlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="order" value="0" /> </bean> <bean id="dwrUrlMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="order" value="1" /> <property name="urlMap"> <util:map> <entry key="/engine.js" value-ref="dwrController"/> <entry key="/interface.js" value-ref="dwrController"/> <entry key="/call/**" value-ref="dwrController"/> <entry key="/interface/**" value-ref="dwrController"/> </util:map> </property> </bean>ayax79
February 11, 2009 at 3:16 pm
Thanks for this post. Do you have any idea of how to configure reverse ajax in DWR with spring?
Shailendra
February 11, 2009 at 8:52 pm
I personally haven’t done this. In the past when I used reverse ajax I used the dwr servlet.
There is some information in this post:
http://www.nabble.com/DWR2—Spring-2.x—Reverse-Ajax-td15865639.html
good luck!
AJ
February 12, 2009 at 9:27 am
Can you show me the full configuration for using DWR in an existing Spring MVC project? thanks
DuhneL
March 6, 2009 at 1:17 am
Hi,
I have a User class with private members: id, lastName, and firstName. however i have the getters/setters public.
What is the right way to write dwr:convert, i am doing something like this:
Anonymous
March 27, 2009 at 4:31 pm
i need the cofiguration of spring in dwr.xml.i heard that using
where projectController and commonDAO are the bean names configured in dispacther-servlet.xml but i am not getting the result can anybody help in this scenerio
thanks in advance
Kishore
March 30, 2009 at 2:06 am