SSH 应用DWR

来源:互联网 发布:树莓派3 查看网络 编辑:程序博客网 时间:2024/05/17 16:16
  DWR是Direct Web Remoting 的缩写。 

     是一个WEB远程调用框架.。页面可通过java业务方法来实现ajax的一个开源框架。

     最初接触dwr,时,感觉最大的方便之处就是,它可以调用java的业务方法和映射java实体类。

    好吧,现在来看看怎么配置dwr框架。dwr也是可以支持注解配置的。通过配置文件和注解两种方式来简单介绍一下dwr在项目的使用。

    我写了俩个demo ,配置文件和注解两种方式各一个 demo,点击下载(eclipse 开发的,使用myeclipse 导入  稍作修改)

      

首先,加入dwr.jar包。配置dwr.xml:


[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE dwr PUBLIC   
  3.     "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"   
  4.     "http://getahead.org/dwr/dwr20.dtd">   
  5. <dwr>  
  6.     <allow>  
  7.          <convert match="com.zuxiang.entity.*" converter="bean">  
  8.             
  9.          </convert> <!-- 这个节点是做转换,指定那些类可转换成在页面可通过对象点属性方式使用的js对象   -->  
  10.            
  11.          <create creator="spring" javascript="customerService">   
  12.   
  13.              <!-- creator 由spring创建, 在页面使用的js,为   customerService.js -->  
  14.   
  15.                   <param name="beanName" value="customerService" /> <!-- 由spring管理的业务类对象 customerService -->  
  16.                   <!--指定可供远程调用的以为方法-->   
  17.                   <include method="findAll" />   
  18.         </create>   
  19.     </allow>  
  20. </dwr>  


[html] view plaincopy
  1. <pre></pre>当然,使用注解是不需要dwr.xml文件的。xml文件里 convert  指定那些类可转换,  
  2. <p></p>  
  3. <p>注解是通过@DataTransferObject,标识那些类可转换,@DataTransferObject是标注类的,就是说只要这个类标注了@DataTransferObject,那么这个这个类就可供js使用,</p>  
  4. <p>但是有时我只想用到某两个属性怎么办?那就在属性上标注@RemoteProperty,如果标注了@RemoteProperty,那没js,可使用的就只有标了@RemoteProperty的字段了</p>  
  5. <p></p>  
  6. <pre name="code" class="java">package com.zuxiang.entity;  
  7.   
  8. import java.util.List;  
  9.   
  10. import javax.persistence.CascadeType;  
  11. import javax.persistence.Column;  
  12. import javax.persistence.Entity;  
  13. import javax.persistence.FetchType;  
  14. import javax.persistence.GeneratedValue;  
  15. import javax.persistence.GenerationType;  
  16. import javax.persistence.Id;  
  17. import javax.persistence.OneToMany;  
  18. import javax.persistence.Table;  
  19.   
  20. import org.directwebremoting.annotations.DataTransferObject;  
  21.   
  22.   
  23.   
  24.   
  25. /**  
  26.  * CusCustomer entity. @author MyEclipse Persistence Tools  
  27.  */  
  28. @Entity  
  29. @Table(name = "customer" ,schema = "cc")  
  30. @DataTransferObject    //这是个实体类,标注了@DataTransferObject ,就可供js,使用  
  31. public class Customer implements java.io.Serializable {  
  32.   
  33.     // Fields  
  34.   
  35.     /**  
  36.      *   
  37.      */  
  38.     private static final long serialVersionUID = 1L;  
  39.     @Id  
  40.     @Column(name = "cus_id")  
  41.     @GeneratedValue(strategy = GenerationType.AUTO)  
  42.     private Long cusId;  
  43.   
  44.          @Column(name = "cus_name" ,length = 20 )  
  45.           // @RemoteProperty 如果只要该属性s  
  46.           private String cusName;  
  47.     @Column(name = "cus_addr",length = 250)  
  48.     private String cusAddr;  
  49.       
  50.     @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer" ,fetch = FetchType.EAGER)  
  51.     private List<Product> products;  
  52.   
  53.     // Constructors  
  54.   
  55.     /** default constructor */  
  56.     public Customer() {  
  57.     }  
  58.   
  59.   
  60.   
  61.     /** full constructor */  
  62.     public Customer(String cusName, String cusAddr) {  
  63.         this.cusName = cusName;  
  64.         this.cusAddr = cusAddr;  
  65.     }  
  66.   
  67.     // Property accessors  
  68.   
  69.     public Long getCusId() {  
  70.         return this.cusId;  
  71.     }  
  72.   
  73.     public void setCusId(Long cusId) {  
  74.         this.cusId = cusId;  
  75.     }  
  76.   
  77.     public String getCusName() {  
  78.         return this.cusName;  
  79.     }  
  80.   
  81.     public void setCusName(String cusName) {  
  82.         this.cusName = cusName;  
  83.     }  
  84.   
  85.     public String getCusAddr() {  
  86.         return this.cusAddr;  
  87.     }  
  88.   
  89.     public void setCusAddr(String cusAddr) {  
  90.         this.cusAddr = cusAddr;  
  91.     }  
  92.       
  93.     public List<Product> getProducts() {  
  94.         return products;  
  95.     }  
  96.   
  97.     public void setProducts(List<Product> products) {  
  98.         this.products = products;  
  99.     }  
  100.   
  101. }</pre><br>  
  102. <p></p>  
  103. <p>那么业务方法又怎么标注呢。。使用@RemoteProxy标注可远程调用的业务类,@RemoteMethod标注远程调用的方法。</p>  
  104. <p>如果使用Spring中逻辑层则需要进行如下的设置:</p>  
  105. <p></p>  
  106. <pre name="code" class="java">@RemoteProxy(creator = SpringCreator.class,  
  107.     creatorParams = @Param(name = "beanName" ,value ="customerService"),  
  108.     name = "customerService")</pre>  
  109. <p></p>  
  110. <p></p>  
  111. <pre name="code" class="java">package com.zuxiang.biz;  
  112.   
  113. import java.util.List;  
  114.   
  115. import javax.annotation.Resource;  
  116.   
  117. import org.directwebremoting.annotations.Param;  
  118. import org.directwebremoting.annotations.RemoteMethod;  
  119. import org.directwebremoting.annotations.RemoteProxy;  
  120. import org.directwebremoting.spring.SpringCreator;  
  121. import org.springframework.stereotype.Service;  
  122. import org.springframework.transaction.annotation.Transactional;  
  123.   
  124. import com.zuxiang.dao.CustomerDao;  
  125. import com.zuxiang.entity.Customer;  
  126.   
  127. @Service  
  128. @Transactional  
  129. @RemoteProxy(creator = SpringCreator.class,  
  130.     creatorParams = @Param(name = "beanName" ,value ="customerService"),  
  131.     name = "customerService")  
  132. public class CustomerServiceImpl implements CustomerService{  
  133.   
  134.     @Resource  
  135.     private CustomerDao customerDao;  
  136.       
  137.   
  138.   
  139.   
  140.     @RemoteMethod  
  141.     public List<Customer> findAll() {  
  142.         // TODO Auto-generated method stub  
  143.         return customerDao.findAll();  
  144.     }  
  145.   
  146.   
  147.   
  148. }  
  149. </pre>下面配置,spring,配置文件,  
  150. <p></p>  
  151. <p>使用 xml.文件的spring配置,就要注意dwr.xml里的 红色部分要对应 spring 里 红色部分;</p>  
  152. <pre name="code" class="html"><param name="beanName"   
  153.                 value="<span style="color:#FF0000;">customerService</span>/> </pre>applicationContext.xml:  
  154. <p></p>  
  155. <p></p>  
  156. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>  
  157. <beans  
  158.     xmlns="http://www.springframework.org/schema/beans"  
  159.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  160.     xmlns:context="http://www.springframework.org/schema/context"  
  161.     xmlns:p="http://www.springframework.org/schema/p"   
  162.     xmlns:aop="http://www.springframework.org/schema/aop"  
  163.     xmlns:tx="http://www.springframework.org/schema/tx"  
  164.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  165.         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  166.         http://www.springframework.org/schema/context   
  167.         http://www.springframework.org/schema/context/spring-context-2.0.xsd   
  168.         http://www.springframework.org/schema/aop   
  169.         http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
  170.         http://www.springframework.org/schema/tx   
  171.         http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  172.           
  173.     <!-- session工厂 -->      
  174.     <bean id="sessionFactory"  
  175.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  176.         <property name="configLocation"  
  177.             value="classpath:hibernate.cfg.xml">  
  178.         </property>  
  179.     </bean>  
  180.       
  181.     <!-- 事务管理 -->  
  182.     <bean id="myHibTransactionManager"  
  183.           class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  184.           <property name="sessionFactory" ref="sessionFactory"></property>  
  185.             
  186.      </bean>  
  187.       
  188.     <!-- dao类 -->  
  189.     <bean id="customerDao" class="com.zuxiang.dao.CustomerDaoImpl">  
  190.         <property name="sessionFactory">  
  191.             <ref bean="sessionFactory" />  
  192.         </property>  
  193.     </bean>  
  194.       
  195.     <!-- 业务类 -->  
  196.     <bean id="<span style="color:#FF0000;">customerService</span>class="com.zuxiang.biz.CustomerServiceImpl">  
  197.         <property name="customerDao" ref="customerDao"/>  
  198.     </bean>  
  199.       
  200.     <!-- struts1 aciton -->  
  201.     <bean name="/cus" class="com.zuxiang.struts.action.CustomerAction">  
  202.         <property name="customerService" ref="customerService"></property>  
  203.     </bean>  
  204.       
  205.       
  206.     <!-- 事务的切面 -->  
  207.       <tx:advice id="txAdvice"  
  208.         transaction-manager="myHibTransactionManager">  
  209.           <tx:attributes>  
  210.         <tx:method name="add*" propagation="REQUIRED" />  
  211.         <tx:method name="del*" propagation="REQUIRED" />  
  212.         <tx:method name="update*" propagation="REQUIRED" />  
  213.         <tx:method name="do*" propagation="REQUIRED" />  
  214.         <tx:method name="*" propagation="SUPPORTS"  read-only="true" />  
  215.     </tx:attributes>  
  216.     </tx:advice>  
  217.       
  218.     <!--注入事务 -->  
  219.     <aop:config>  
  220.         <aop:pointcut id="bizMethods"  
  221.         expression="execution(* com.zuxiang.biz.*.*(..))" />  
  222.         <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />  
  223.     </aop:config>  
  224.       
  225.       
  226.     </beans></pre><br>  
  227. 使用注解的spring,applicationContext.xml:  
  228. <p></p>  
  229. <p></p>  
  230. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>  
  231. <beans xmlns="http://www.springframework.org/schema/beans"   
  232.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  233.     xmlns:p="http://www.springframework.org/schema/p"   
  234.     xmlns:context="http://www.springframework.org/schema/context"  
  235.     xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"    
  236.     xmlns:tx="http://www.springframework.org/schema/tx"  
  237.     xmlns:aop="http://www.springframework.org/schema/aop"  
  238.     xsi:schemaLocation="  
  239.         http://www.springframework.org/schema/beans   
  240.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  241.         http://www.springframework.org/schema/context   
  242.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  243.        http://www.springframework.org/schema/aop   
  244.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  245.        http://www.springframework.org/schema/tx   
  246.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  247.        http://www.directwebremoting.org/schema/spring-dwr      
  248.        http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">  
  249.                     
  250.   <context:annotation-config />    
  251.   <!-- 使用注解配置 -->  
  252.   <dwr:configuration />    
  253.   <dwr:annotation-config />    
  254.   <dwr:url-mapping />    
  255.     
  256.   <!-- 自动扫描指定包下的类 -->  
  257.   <context:component-scan base-package="com.zuxiang">  
  258.      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />   
  259.   </context:component-scan>  
  260.     
  261.   <!-- jpa实体管理类 -->  
  262.   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">  
  263.       <property name="persistenceUnitName" value="zuxiang"/>  
  264.   </bean>    
  265.     
  266.   
  267.    <!-- 事务管理 -->  
  268.   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
  269.         <property name="entityManagerFactory" ref="entityManagerFactory"/>  
  270.   </bean>  
  271.   
  272.   <!-- dwr 扫描转换类和远程调用类 -->  
  273.    <dwr:annotation-scan scanDataTransferObject="true" scanRemoteProxy="true" base-package="com.zuxiang"/>    
  274.    
  275.   <!-- debug 模式 -->  
  276.    <dwr:controller id="dwrController" debug="true"></dwr:controller>    
  277.     
  278.     
  279.   <!-- 使用注解事务 -->  
  280.   <tx:annotation-driven transaction-manager="transactionManager"/>  
  281.     
  282. </beans></pre><br>  
  283. 下面是 web.xml 配置文件,  
  284. <p></p>  
  285. <p>配置文件方式的的web.xml:</p>  
  286. <p></p>  
  287. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>  
  288. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  289.   <display-name>dwr2</display-name>  
  290.   <context-param>  
  291.     <param-name>contextConfigLocation</param-name>  
  292.     <param-value>classpath:applicationContext.xml</param-value>  
  293.   </context-param>  
  294.   <listener>  
  295.     <description>register spring listener</description>  
  296.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  297.   </listener>  
  298.     
  299.   <filter>  
  300.     <filter-name>MyFilter</filter-name>  
  301.     <filter-class>com.zuxiang.filter.MyFilter</filter-class>  
  302.   </filter>  
  303.   <filter-mapping>  
  304.     <filter-name>MyFilter</filter-name>  
  305.     <url-pattern>*.do</url-pattern>  
  306.   </filter-mapping>  
  307.   <servlet>  
  308.     
  309.   <!-- struts1的aciton -->  
  310.    <servlet-name>action</servlet-name>  
  311.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>  
  312.     <init-param>  
  313.       <param-name>config</param-name>  
  314.       <param-value>/WEB-INF/struts-config.xml</param-value>  
  315.     </init-param>  
  316.     <init-param>  
  317.       <param-name>debug</param-name>  
  318.       <param-value>3</param-value>  
  319.     </init-param>  
  320.     <init-param>  
  321.       <param-name>detail</param-name>  
  322.       <param-value>3</param-value>  
  323.     </init-param>  
  324.     <load-on-startup>0</load-on-startup>  
  325.   </servlet>  
  326.   <servlet-mapping>  
  327.     <servlet-name>action</servlet-name>  
  328.     <url-pattern>*.do</url-pattern>  
  329.   </servlet-mapping>  
  330.   <servlet>  
  331.   <!-- struts1的aciton -->  
  332.     
  333.     
  334.   <!-- dwr 配置 -->  
  335.     <servlet-name>dwr</servlet-name>  
  336.     <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>  
  337.     <init-param>  
  338.       <param-name>debug</param-name>  
  339.       <param-value>true</param-value>  
  340.     </init-param>  
  341.     <init-param>  
  342.       <param-name>crossDomainSessionSecurity</param-name>  
  343.       <param-value>false</param-value>  
  344.     </init-param>  
  345.   </servlet>  
  346.   <servlet-mapping>  
  347.     <servlet-name>dwr</servlet-name>  
  348.     <url-pattern>/dwr/*</url-pattern>  
  349.   </servlet-mapping>  
  350.   <!-- dwr 配置 -->  
  351.     
  352.     
  353.   <welcome-file-list>  
  354.     <welcome-file>index.html</welcome-file>  
  355.     <welcome-file>index.htm</welcome-file>  
  356.     <welcome-file>index.jsp</welcome-file>  
  357.     <welcome-file>default.html</welcome-file>  
  358.     <welcome-file>default.htm</welcome-file>  
  359.     <welcome-file>default.jsp</welcome-file>  
  360.   </welcome-file-list>  
  361. </web-app></pre><br>  
  362. 注解方式的web.xml:  
  363. <p></p>  
  364. <p></p>  
  365. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>  
  366. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  367.   <display-name>dwr3</display-name>  
  368.     
  369.   <context-param>  
  370.         <param-name>contextConfigLocation</param-name>  
  371.         <param-value>classpath:applicationContext.xml</param-value>  
  372.   </context-param>  
  373.     
  374.   <listener>  
  375.         <description>register spring listener</description>  
  376.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  377.   </listener>  
  378.     
  379.   <!-- struts2 配置 -->  
  380.   <filter>  
  381.         <filter-name>struts2</filter-name>  
  382.         <filter-class>  
  383.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  384.         </filter-class>  
  385.   </filter>  
  386.     
  387.   <filter-mapping>  
  388.         <filter-name>struts2</filter-name>  
  389.         <url-pattern>/*</url-pattern>  
  390.   </filter-mapping>  
  391.     <!-- struts2 配置 -->  
  392.       
  393.     <!-- dwr 配置 -->  
  394.   <servlet>    
  395.        <servlet-name>dwr</servlet-name>    
  396.           <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>    
  397.        <init-param>    
  398.          <param-name>debug</param-name>    
  399.          <param-value>true</param-value>    
  400.        </init-param>    
  401.        <!-- 解决CSRF Security Error  加入下面 加入跨域调用配置信息 -->  
  402.       <init-param>  
  403.              <param-name>crossDomainSessionSecurity</param-name>  
  404.              <param-value>false</param-value>  
  405.       </init-param>  
  406.       <init-param>  
  407.             <param-name>allowScriptTagRemoting</param-name>  
  408.             <param-value>true</param-value>  
  409.       </init-param>  
  410.          
  411.   </servlet>    
  412.     
  413.   <servlet-mapping>    
  414.       <servlet-name>dwr</servlet-name>    
  415.       <url-pattern>/dwr/*</url-pattern>    
  416.   </servlet-mapping>    
  417.   
  418.     
  419.    <!-- dwr 配置 -->  
  420.     
  421.   <welcome-file-list>  
  422.     <welcome-file>index.html</welcome-file>  
  423.     <welcome-file>index.htm</welcome-file>  
  424.     <welcome-file>index.jsp</welcome-file>  
  425.     <welcome-file>default.html</welcome-file>  
  426.     <welcome-file>default.htm</welcome-file>  
  427.     <welcome-file>default.jsp</welcome-file>  
  428.   </welcome-file-list>  
  429. </web-app></pre><br>  
  430. 这里只是说一下相关配置,到我资源里下载demo,运行一下。更容易看懂<br>  
  431. <p></p>  
  432. <p><br>  
  433. </p>  
  434. <p><br>  
  435. </p>  
  436. <p><br>  
  437. </p>  
  438. <p><br>  
  439. </p>  
  440. <p><br>  
  441. </p>  
  442. <br>  
  443. <pre></pre>  
原创粉丝点击