How to integrate DWR with Spring using Annotation:
Requirements:
<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:
Requirements:
- Java 1.5 or more
- Spring 3.x
- DWR 3.X
- 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"
- Have the folowing tag after spring annotation classes like below.
- <dwr:annotation-scan base-package="basepackage.dwr" scanDataTransferObject="true" scanRemoteProxy="true" />
- scanRemoteProxy - to scan the RemoteProxy classes
- scanDataTransferObject - to scan the DTO object for page.
- Add the below tags.
- <dwr:configuration />
- <dwr:annotation-config /> - This is basically telling Spring that its an annoatated DWR 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:
- DWR will create proxy classes at runtime and you can link them like http://servername:port/application/dwr/interface/yourclass.js
- Access the method in javascript similar to static methods in java.
- Provide the callbackhandlers as an arument and callbackhandler will be an javascript method.
1 comment:
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
Post a Comment