A.J.

Someday somehow someone is going to steal your carbon.

Posts Tagged ‘dwr

Spring configuration of DWR.

with 8 comments

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.

Written by AJ

December 4, 2007 at 10:24 am

Posted in java

Tagged with ,

Follow

Get every new post delivered to your Inbox.