打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用annotation注解,整合DWR3 + Spring3 + Hibernate3

首先参考了wangcheng的一个博客:http://wangcheng.iteye.com/blog/409548

 

因为我的Spring已经是使用annotation注解,所以现在加入DWR框架一样使用annotation注解,Spring、Hibernate文件配置这里不做解释。

1.系统的业务类:

 

Java代码  
  1. package com.gxuwz.shiplock.mainservice.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javacommon.base.BaseManager;  
  6. import javacommon.base.EntityDao;  
  7.   
  8. import org.directwebremoting.annotations.RemoteMethod;  
  9. import org.directwebremoting.annotations.RemoteProxy;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11. import org.springframework.stereotype.Service;  
  12. import org.springframework.transaction.annotation.Transactional;  
  13.   
  14. import com.gxuwz.shiplock.mainservice.dao.ServiceRegisterDao;  
  15. import com.gxuwz.shiplock.mainservice.model.ServiceRegister;  
  16. import com.gxuwz.shiplock.mainservice.vo.service.SchedulingParameters;  
  17.   
  18. /** 
  19.  * @author  梧州学院 软件开发中心 庞光垚  116861519@qq.com 
  20.  * @version  1.0 
  21.  * <br>Copyright (C), 2012, 梧州学院 软件开发中心 
  22.  * <br>This program is protected by copyright laws. 
  23.  * <br>Program Name: 船闸调度管理信息系统 
  24.  * @since 1.0 
  25.  */  
  26. @Service  
  27. @Transactional  
  28. <span style="color: #ff0000;">@RemoteProxy</span>  
  29. public class ShipLockSchedulManager extends BaseManager<ServiceRegister,Long>{  
  30.     //通过autowire自动设置对象属性  
  31.     @Autowired  
  32.     private ServiceRegisterDao serviceRegisterDao;  
  33.       
  34.     public EntityDao getEntityDao() {  
  35.         return this.serviceRegisterDao;  
  36.     }  
  37.       
  38.     /** 
  39.      * 根据条件查询调度列表,用于船闸调度页面“已登记”列表 
  40.      * @param schedulStatus 状态 
  41.      * @param schedulDirection 方向 
  42.      * @param sroom 闸室,-1为忽略当前 条件 
  43.      * @param SchedulingParameters 调度条件列表 
  44.      * @return 调度信息列表 
  45.      */  
  46.     @Transactional(readOnly=true)  
  47.     @RemoteMethod  
  48.     public List<ServiceRegister> findRegisteredForDoScheduling(long schedulStatus,long schedulDirection,long sroom,SchedulingParameters schedulingParameters){  
  49.         return serviceRegisterDao.findRegisteredForDoScheduling(schedulStatus, schedulDirection,sroom, schedulingParameters);  
  50.     }  
  51.   
  52.     <span style="color: #ff0000;">@RemoteMethod</span>  
  53.     public String sayHello(String name){  
  54.         return "您好!"+name;  
  55.     }  
  56. }  

      @RemoteProxy注解告诉DWR,这个Class是我们想暴露出来的。@RemoteMethod注解告诉DWR去暴露这个指定的方法,只有加了RemoteMethod注解的方法会被暴露,其它的不会。

      这里也可以使用@RemoteProxy(name="userRemote")的方式指定DWR接口的名字。

      以上红色部分是DWR注解,后面加的,其他是之前我们系统的Spring注解。

2.接下来上web.xml

 

Xml代码  
  1.    <!-- dwr 配置 -->  
  2. <servlet>  
  3.     <servlet-name>dwr</servlet-name>  
  4.     <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>          
  5.     <init-param>  
  6.         <param-name>debug</param-name>  
  7.         <param-value>true</param-value>  
  8.     </init-param>  
  9. </servlet>  
  10. <servlet-mapping>  
  11.     <servlet-name>dwr</servlet-name>  
  12.     <url-pattern>/dwr/*</url-pattern>  
  13. </servlet-mapping>  

 web.xml关于Spring等的其他配置文件这里不解释,直接把上段放到web.xml的最后面即可。为了区分原来的URL,我们使用dwr开头的url。

3.最重要的是Spring的配置文件

 

Java代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns = "http://www.springframework.org/schema/beans"  
  3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"  
  4. xmlns:dwr = "http://www.directwebremoting.org/schema/spring-dwr"  
  5. xmlns:context = "http://www.springframework.org/schema/context"  
  6. xmlns:aop = "http://www.springframework.org/schema/aop"  
  7. xsi:schemaLocation = "http://www.springframework.org/schema/beans  
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9. http://www.springframework.org/schema/context  
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11. http://www.directwebremoting.org/schema/spring-dwr  
  12. http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd  
  13. http://www.springframework.org/schema/aop  
  14. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-autowire="byName" default-lazy-init="false">  
  15.   
  16.     <!-- 隐式注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor -->   
  17.     <!-- <context:component-scan base-package = "com.gxuwz.shiplock.**.service" />  -->  
  18.   
  19.     <!-- 扫描 spring 注解的类 , 使其成为客户端调用接口 -->  
  20.     <dwr:annotation-config id="dwrid"/>  
  21.     <!-- 扫描需要转换的 java 对象   
  22.         <dwr:annotation-scan scanRemoteProxy = "false" base-package = "com.gxuwz.shiplock.**.model" /> -->   
  23.     <!-- dwr 初始化配置  
  24.     <dwr:configuration >      
  25.     </dwr:configuration > -->   
  26.       
  27.     <!-- 注意这里新增加的dwr tag, 为使其生效,文件头中要声明namespace -->  
  28.     <dwr:configuration />  
  29.     <dwr:url-mapping />  
  30.       
  31.     <!-- 部署项目时, 请把debug设为false -->  
  32.     <dwr:controller id="dwrController" debug="false" />  
  33.       
  34.     <!-- 多个包名用逗号隔开, 但不能有空格 -->  
  35.     <context:component-scan base-package="com.gxuwz.shiplock.**.service" />  
  36.       
  37.     <!-- order值越小, 优先级越高   
  38.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">  
  39.         <property name="order" value="1" />  
  40.     </bean>  
  41.     <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">  
  42.         <property name="order" value="2" />  
  43.     </bean>-->  
  44. </beans>   

      为了跟之前的系统代码相分离,我新建立了一个xml文件,叫“applicationContext-dwr.xml”,内容就是上面的xml文件。

简单解释一下这些配置

 ·<dwr:annotation-config /> 要求DWR在Spring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类。注意它不会去检查Spring容器之外的类。

 ·<dwr:url-mapping /> 要求DWR将util.js和engine.js映射到dwrController

 ·<dwr:controller id="dwrController" debug="true" /> 定义dwrController

 ·<dwr:configuration /> 此标签在这个例子中不是必须的,如果你想配置Spring容器之外的类,就需要它了。

 

总结:

      至此,配置完成。

      访问:http://127.0.0.1:8080/SHIPLOCK/dwr/test/ShipLockSchedulManager,如果看到以下内容,说明已经成功了。http://127.0.0.1:8080/SHIPLOCK/dwr/test/这一段是固定的,后面那个是java类的名称。

Methods For: ShipLockSchedulManager。。。。。 

最后我们写一下测试页面:

 

Html代码  
  1. <%@ page contentType="text/html;charset=UTF-8" %>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  3. <%@ taglib tagdir="/WEB-INF/tags/simpletable" prefix="simpletable"%>  
  4. <%@ include file="/commons/taglibs.jsp" %>  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7. <head>  
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  9. <title>无标题文档</title>  
  10. <script type='text/javascript' src='${ctx}/dwr/engine.js'></script>  
  11. <script type='text/javascript' src='${ctx}/dwr/interface/ShipLockSchedulManager.js'></script>  
  12. <script type='text/javascript' src='${ctx}/dwr/util.js'></script>  
  13.   
  14. <script type="text/javascript">  
  15. <!--  
  16. function getService(){  
  17.        ShipLockSchedulManager.sayHello('肥猪',function (result){  
  18.            alert(result);   
  19.        });  
  20.     }   
  21. //-->  
  22. </script>  
  23. </head>  
  24.   
  25. <body>  
  26. <input type="button" value=" 获取 Service 对象 " onclick="getService()">   
  27. </body>  
  28. </html>  

 运行一下:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring 注解学习手札
在Spring 2中整合DWR 2
二、spring mvc模拟用户增删改查以及登录和上传文件的相关流程
SpringMVC学习系列(2) 之 经典的HelloWorld实现
activiti入门六(集成新版Activiti Modeler与Rest服务)
使用 Spring 2.5 基于注解驱动的 Spring MVC
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服