我了解过官网关于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);
});
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yyb_gz/archive/2010/04/21/5511164.aspx