Thursday, January 12, 2012

Annotation based DWR Configuration in Spring

How to integrate DWR with Spring using Annotation:
Requirements:
  1. Java 1.5 or more
  2. Spring 3.x
  3. DWR 3.X
 Steps:
  1. In the applicationContext.xml provide the xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation="http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
  2. Have the folowing tag after spring annotation classes like below.
    1.     <dwr:annotation-scan base-package="basepackage.dwr" scanDataTransferObject="true" scanRemoteProxy="true" /> 
    2. scanRemoteProxy - to scan the RemoteProxy classes
    3. scanDataTransferObject - to scan the DTO object for page.
  3. Add the below tags.
    1. <dwr:configuration />
    2. <dwr:annotation-config /> - This is basically telling Spring that its an annoatated DWR config.
Bean Config:
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
    xsi:schemaLocation="http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 // This is to inject spring beans for DWR pacakage. 
    <context:component-scan base-package="com.jlr.onlinereservation.dwr" />
    <dwr:annotation-scan base-package="com.jlr.onlinereservation.dwr" scanDataTransferObject="true" scanRemoteProxy="true" />
      
    <dwr:configuration />

    <dwr:annotation-config />
</beans>

Java Class

package mypacakge.dwr;

import java.util.ArrayList;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;

import mypacakge.model.User;
import mypacakge.service.UserService;

@RemoteProxy
public class TestBean
{
    @Autowired
    private UserService userService;
   
    @RemoteMethod
    public List<Integer> getStuff()
    {
    User user = userService.get(1);
    System.out.println(user.getUserName());
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < 100; i++)
    {
        list.add(i);
    }
    return list;
    }

    public void setUserService(UserService userService)
    {
        this.userService = userService;
    }
}
 

Testing DWR:
You can access the DWR classes by accessing http://servername:port/application/dwr/index.html

Accessing in JSP:
  1. DWR will create proxy classes at runtime and you can link them like http://servername:port/application/dwr/interface/yourclass.js
  2. Access the method in javascript similar to static methods in java.
  3. Provide the callbackhandlers as an arument and callbackhandler will be an javascript method.

1 comment:

Anonymous said...

Hello, How do I get in touch with you? There is no email or contact info listed .. please advise .. thanks .. Mary. Please contact me maryregency at gmail dot com