Question

I have a web application that integrates DWR 3 and Spring 3. All requests are handled by the Spring's DispatcherServlet. Everything works. When I request an AJAX request, it's handled correctly by the backing DWR service bean annotated with @RemoteProxy. To return a response, my DWR service bean returns either an HTML string including the @DataTransferObject POJO or just the plain POJO.

What I want to do is in the DWR service bean I want it to forward the processing to a Spring @Controller bean. The AJAX request will still be processed by the DWR service but the real processing is delegated to the Spring controller bean. In other words the DWR service bean is just a service facade to the actual service. In this way I'm not duplicating logic.

Is this possible?

Let me clarify further.

In a normal non-AJAX application, when a user submits a form, here's what happens:

  1. It's forwarded to the DispatcherServlet
  2. Then to an @Controller annotated bean.
  3. The processing is then handled by @Service bean.
  4. Afterwards, the controller returns a ModelAndView.

In a DWR-AJAX application, when a user submits a form, here's what happens:

  1. It's forwarded to the DispatcherServlet still
  2. Then to a @RemoteProxy annotated bean. The processing is handled by this bean. That's DWR's service bean.
  3. Afterwards, this remote proxy bean returns either an @DataTransferObject POJO or just plain HTML string

Basically for the AJAX application, after step 2, I want it to forward to the @Controller bean so that everything is still processed by Spring.

Was it helpful?

Solution

Short answer is no.

There is no place in a DWR request for a spring controller. DWR has great support for remoting spring beans, but there is little value that a controller could bring to this equation.

That said there is no reason that I can think of that a spring 3.0 style annotated controller couldn't be used as the exposed DWR bean. The restriction would be that the return type would not be ModelAndView rather it would just be your model object it's self.

OTHER TIPS

I don't know much about using annotations in this case. With the declarative approach you can do the following.

In spring XML(Note that fileOperationService is the spring bean)

    <bean name="BookRequestAjax" class="com.bookie.struts.BookRequestAjax">
        <property name="fileOperationService" ref="fileOperationService"/>
    </bean>

Your bean

public class BookRequestAjax {
    FileOperationService fileOperationService;
    public void deleteFile(String fileName){
        try{
            fileOperationService.deleteFile(fileName);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void setFileOperationService(FileOperationService fileOperationService) {
        this.fileOperationService = fileOperationService;
    }

}

Your DWR.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
    <allow>
    <create creator="spring" javascript="BookRequestAjax">
      <param name="beanName" value="BookRequestAjax" />
    </create>
    </allow>
</dwr>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top