我了解过官网关于 dwr3 的介绍后,发现它介绍的配置 dwr 的方法不是与 spring 结合的。也就是说每次进行 dwr 访问,系统都会重新 new 一个 service 来异步处理我们的请求。这对于简单的功能是没什么问题的,但如果涉及到复杂的逻辑处理,特别是需要 synchronized 的场合,就会出现问题了。所以我在网上专门找把 dwr3 与 spring 结合的配置方法。现在总结一下配置方法。
web.xml 的配置:
< servlet >
< servlet-name > dwr </ servlet-name >
< servlet-class >
org.directwebremoting.spring.DwrSpringServlet
</ servlet-class >
< init-param >
< param-name > debug </ param-name >
< param-value > true </ param-value >
</ init-param >
</ servlet >
< servlet-mapping >
< servlet-name > dwr </ servlet-name >
< url-pattern > /dwr /* </ url-pattern >
</ servlet-mapping >
注意: org.directwebremoting.spring.DwrSpringServlet 与 dwr2.x 官网介绍的与 spring 结合的那个 servlet 一样 .
applicationContext.xml 的配置:
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:dwr = "http://www.directwebremoting.org/schema/spring-dwr"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/ spring-dwr-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
<!-- 隐式注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor -->
< context:component-scan base-package = " service " />
<!-- 扫描 spring 注解的类 , 使其成为客户端调用接口 -->
< dwr:annotation-config />
<!-- 扫描需要转换的 java 对象 -->
< dwr:annotation-scan scanRemoteProxy = "false" base-package = " entity " />
<!-- dwr 初始化配置 -->
< dwr:configuration ></ dwr:configuration >
< bean id = "loginService" class = "service.LoginService" >
< property name = "sessionFactory" ref = "sessionFactory" ></ property >
</ bean >
</ beans >
注意:
1. 文件头说明是 dwr3 的应用。
2. 红色字体的“ service ”指出 dwr 调用的服务类所在的包名。 LoginService 是我例子中处理 dwr 请求的服务类,它所在的包名就是“ service ”。
3. 红色字体的“ entity ”指出 dwr 调用传递的类对象的所在的包名。 User 是我例子中 dwr 传递的对象类,它所在的包名就是“ entity ”。
LoginService 服务类的定义:
package service;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;
@Service
@RemoteProxy
public class LoginService extends HibernateDaoSupport {
@RemoteMethod
public String getService(){
return this.toString();
}
}
注意:使用的注解与 dwr 官网关于 dwr3 的介绍一样
调用 dwr 的 index.jsp 的相关内容如下:
1 . <input type="button" value=" 获取 Service 对象 " οnclick="getService()">
2 . < script type = 'text/javascript' src = '/struts2_spring2_ahibernate3_dwr3/dwr/interface/LoginService.js' ></ script >
< script type = 'text/javascript' src = '/struts2_spring2_ahibernate3_dwr3/dwr/engine.js' ></ script >
3 . function getService(){
LoginService.getService( function (result){
alert(result);
});
}