Skip to content

DWR Annotations & Spring

2007 March 15
tags: , ,
by Joe

We recently upgraded DWR in our primary product from version 1.1.4 to version 2.0rc2. The newest version offers a pile of great improvements on an already excellent piece of code. One of my largest motivations for the upgrade was the new annotation-based configuration option and the resulting elimination of our dwr.xml file that had grown to be large and confusing. The signatures block can be completely dropped with Java 1.5 and the create and convert elements can now be represented 100% in annotations.

Our codebase was built with Spring in mind from the start and I hit a small block when trying to get the annotations to work. This page provides a start but doesn’t specifically mention Spring. Here is what an annotated RemoteProxy class looks like with Spring factored in:


import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.spring.SpringCreator;

/**
* A Java controller exposed via DWR
*/
@RemoteProxy(
    creator = SpringCreator.class,
    creatorParams = @Param(name = "beanName", value = "someAjaxController"),
    name = "someAjaxController"
)
public class SomeAjaxController{

    @RemoteMethod
    public int trivialExample(int param1, int param2){
        return param1 + param2;
    }
}

That’s it. As someone who lived mostly in the client-side of DWR I wasn’t familiar with the creator concept and it took me more than a single google run to find the creator/creatorParams being used clearly in the RemoteProxy annotation. In our project we standardized on making the beanName and Javascript name the same but they could be different in the above example.

Hopefully this saves someone else a few additional minutes of searching. I hope to post about Spring and DWR more in the future – they are both excellent projects with experienced, helpful communities.

6 Responses
  1. ojs permalink
    March 27, 2007

    Thanks for this article! How do you initialize DWR – via web.xml or via spring? I’d like to initialize it via spring, because my AjaxController needs a backend service injected.

  2. March 27, 2007

    We’re using spring to initialize DWR. I apologize for the omission. Without clarifying that my post isn’t very helpful to someone getting setup for the first time.

    It looks roughly like this is our application, I will try to make a legitimate post in the future with more details. We started out with version 1.0 of DWR and I believe there may be easier ways to do this initialization now and this excludes some custom properties and subclasses we use and thus may not work without tweaking:


    <bean id="dwrController" class="org.springframework.web.servlet.mvc.ServletWrappingController">
        <property name="servletClass" value="org.directwebremoting.servlet.DwrServlet"/>
        <property name="initParameters">
            <props>
                <prop key="classes">
                    com.sample.Class1,
                    com.sample.Class2,
                    ...
                </prop>
            </props>
        </property>
    </bean>

    The comma-delimited classes parameter is where you list classes annotated for DWR configuration. If you are using dwr.xml for configuration this won’t be necessary.

    Bram Smeets has many very helpful posts about Spring and DWR. Here is his blog. The DWR mailing is also very responsive and helpful – I’d give that a shot if you get stuck.

  3. Peter Odéus permalink
    April 14, 2007

    Nice post.

    It took a while to get things working, though. Here are some issues that wasn’t obvious at first. Here is my story:

    1. web.xml, as pertaining to dwr, now looks like this:

    dwr

    org.springframework.web.servlet.DispatcherServlet

    2

    dwr
    /dwr/*

    2. When having a @RemoteMethod that takes class instance Foo and returns Foo, not only should class Foo be annotated with @DataTransferObject (import org.directwebremoting.annotations.DataTransferObject;). Foo must also be appended to the “classes” property (see DwrServlet’s initParameters above). So basically, ALL classes having dwr annotations MUST be in the “classes” parameter.

    3. To enable debug pages to be reachable from http://whatever.com/dwr, another property must be appended to DwrServlet’s initParameters:


    true

  4. Peter Odéus permalink
    April 14, 2007

    Hmm, I haven’t posted a blog comment before. Ok, let’s try one more time without using code tags:

    It took a while to get things working, though. Here are some issues that wasn’t obvious at first. Here is my story:

    1. web.xml, as pertaining to dwr, now looks like this:

    dwr

    org.springframework.web.servlet.DispatcherServlet

    2

    dwr
    /dwr/*

    2. When having a @RemoteMethod that takes class instance Foo and returns Foo, not only should class Foo be annotated with @DataTransferObject (import org.directwebremoting.annotations.DataTransferObject;). Foo must also be appended to the “classes” property (see DwrServlet’s initParameters above). So basically, ALL classes having dwr annotations MUST be in the “classes” parameter.

    3. To enable debug pages to be reachable from http://whatever.com/dwr, another property must be appended to DwrServlet’s initParameters:
    true

  5. April 14, 2007

    Hey Peter,

    Really appreciate the comment. WordPress is a bear with respect to getting code into posts. I am still using the generic hosted wordpress and as far as I can tell am not allowed to install the plugins that make it easier.

    I’ve been using some Javascript converter that wraps the stuff in a code tag and then converts symbols to their html entity equivalents. It is less than ideal. I need to just setup a box a home and host wordpress myself.

    With that said, I apologize for not mentioning the @DataTransferObject in my post. That is a pretty important part of getting it to work. On our particular project after a handful of custom converters and enum converters in dwr.xml we had a lazy (and potentially dangerous) line that looked like this:

    <convert converter="bean" match="com.ourproject.*"/>

    We had probably 100 different classes that would have needed @DataTransferObject annotations and I just wasn’t up to the task. So we are running DWR in a bit of a hybrid mode – the exposed controllers are configured through annotations and the objects we send over the wire are configured in dwr.xml through that messy wildcard converter line.

    Thanks again for the comment,

    Joe

  6. Andriy permalink
    January 12, 2009

    Hello Joe/Peter,

    I am unable to get Spring 2.5 and DWR 2.0 setup when DWR is configured using annotations. I am able to get them going the usual xml way (with dwr namespaces in spring configurarion file). Could either of you please post some more details on how to configure DWR using annotations?

    Thanks!

Comments are closed.