Dwr 学习研究应用--DWR的注解的使用

xiaoxiao2026-04-18  2

DWR2.0新增加了JDK5的注解(Annotation)功能,使用注解功能之后可以从很大程度上简化了原来的dwr.xml的配置,使用起来非常的方便。

一、配置DWR使用注解功能

如果让dwr使用注解功能需要在web.xml进行如下配置:

<servlet>         <servlet-name>dwr-invoker</servlet-name>         <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>         <init-param>             <param-name>debug</param-name>             <param-value>true</param-value>         </init-param>         <!-- 将设定注解的域对象和业务实现类放在下面列表中,中间使用逗号间隔 -->         <init-param>             <param-name>classes</param-name>             <param-value>                 com.javatang.domain.Book,                com.javatang.bank.Bank,                com.javatang.dao.BookDao             </param-value>         </init-param>     </servlet>     <servlet-mapping>         <servlet-name>dwr-invoker</servlet-name>         <url-pattern>/scripts/ajax/*</url-pattern>     </servlet-mapping>

这样设置之后就可以在dwr中使用注解了,非常的方便。

二、DWR提供的注解类型经常用到的注解主要有:@RemoteProxy、@RemoteMethod、@DataTransferObject和@RemoteProperty。

1. @RemoteProxy和@RemoteMethod@RemoteMethod对应于原来dwr.xml文件中的create标签,用于创建DWR所提供的远程方法;而@RemoteMethod对应于create标签中的 <include method=”"/>,用来指定所要暴露的方法名称。我们举例来说明:

@RemoteProxy(name="bankFunctions") public class  Bank{       @RemoteMethod     public voidbuy(){         // ...     } }

从上面可以看出,@RemoteProxy表示这个类将用于远程方法,而使用@RemoteMethod指定所要暴露的方法,没有使用@RemoteMethod的方法将不会显示在客户端。上面的注释使用dwr.xml表示如下:

<!DOCTYPEdwrPUBLIC     "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"     "http://getahead.ltd.uk/dwr/dwr20.dtd"> <dwr>     <allow>         <create creator="new"javascript="bankFunctions">             <include method="buy"/>         </create>     </allow> </dwr>

如果使用Spring中的DAO活逻辑层则需要进行如下的设置:

// BookDao @RemoteProxy(creator = SpringCreator.class,     creatorParams = @Param(name = "beanName", value = "bookDao"),     name="bookFunctions") public class  BookDao{       @RemoteMethod     public voidaddBook(Bookbook){         // ...     } }

通过指定@RemoteProxy中的creator类型为SpringCreator,然后在creatorParams指定对应的beanName名称。对应的dwr.xml文件如下:

<!DOCTYPEdwrPUBLIC     "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"     "http://getahead.ltd.uk/dwr/dwr20.dtd"> <dwr>     <allow>         <create creator="spring"javascript="bookFunctions">             <param name="beanName"value="bookDao"/>             <include method="addBook"/>         </create>     </allow> </dwr>

2. @DataTransferObject和@RemoteProperty@DataTransferObject对应于原来dwr.xml文件中的convert标签,用于转换Java对象;@RemoteProperty则对应于convert标签中的 <param name=”include” value=”" />。

举例说明一下:

@DataTransferObject public classBook{     @RemoteProperty     private intid;       @RemoteProperty     private Stringname;       public Book(){     }       public intgetId(){         return id;     }       public voidsetId(intid){         this.id = id;     }       public StringgetName(){         return name;     }       public voidsetName(Stringname){         this.name = name;     } }

@RemoteProperty可以放在JavaBean中的私有变量上面,也可以放在getXXX方法上面。另外如果想将JavaBean中所有的属性都暴露出来的话,不需要在任何属性上面添加@RemoteProperty注释就可以了。

上面的注释对应的dwr.xml文件如下:

<!DOCTYPEdwrPUBLIC     "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"     "http://getahead.ltd.uk/dwr/dwr20.dtd"> <dwr>     <allow>         <convert converter="bean"             match="com.javatang.domain.Book">             <param name="include"value="id, name"/>         </convert>         <!-- 或者用下面的方式也可以         <convert converter="bean" match="com.javatang.domain.Book" />        -->     </allow> </dwr>

关于具体每个注释使用的方法已经所包含的参数可以参考Java Doc。使用DWR2.0的注解极大的简化了原来dwr.xml的配置,非常的方便。

 

总结:

 针对DWR对Javabean的注解分为两类:

Object和Class

针对Class的注解使用:

@RemoteObject注解类为远程反问class相等于create

@RemoteMethod 注解类为远程访问方法相等于dwr.xml中白名单中注解的方法的应用的

注意默认的类中所有的方法为全部在黑名单中。不可作为远程访问对象的。

 

针对:Object使用@DataTransferObject 和@RemoteProperty注解接口

@DataTransferObject仅仅注解类对象

@RemoteProperty注解属性和方法的应用

 

 

 

使用远程访问类的

package cn.com.unusap.dwr.annotation;

import org.directwebremoting.annotations.RemoteMethod;import org.directwebremoting.annotations.RemoteProxy;/** * 注解此类为远程访问对象的应用 * @author longgangbai * */@RemoteProxy(name = "DWRAnnotation")public class DWRAnnotation { /**  * 注解此方法为远程方法的  * @param username  * @return  */ @RemoteMethod public String hello(String username) {  return "Hello ," + username; }}

web.xml的配置信息:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet>  <servlet-name>dwr</servlet-name>  <servlet-class>   org.directwebremoting.servlet.DwrServlet  </servlet-class>  <!-- 用於測試dwr的模式 -->  <init-param>    <param-name>debug</param-name>    <param-value>true</param-value>  </init-param>  <!-- 针对注解类必须使用的远程访问对象的描述 -->  <init-param>   <param-name>classes</param-name>   <param-value>    cn.com.unusap.dwr.annotation.DWRAnnotation   </param-value>     </init-param> </servlet> <servlet-mapping>  <servlet-name>dwr</servlet-name>  <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list>  <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>

相应的jsp页面用于测试注解的使用

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://"   + request.getServerName() + ":" + request.getServerPort()   + path + "/";%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <base href="<%=basePath%>">

  <title>My JSP 'index.jsp' starting page</title>  <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">

  <script type='text/javascript'   src='/DWRAnnotation/dwr/interface/DWRAnnotation.js'></script>  <script type='text/javascript' src='/DWRAnnotation/dwr/engine.js'></script>  <script type='text/javascript' src='/DWRAnnotation/dwr/util.js'></script>

  <script type="text/javascript">    function helloMessage()    {      var username=DWRUtil.getValue("username");      DWRAnnotation.hello(username,function(data)      {        DWRUtil.setValue("message",data);      });    }  </script>

  <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->

 </head>

 <body>  UserName:  <input type="text" id="username" name="username" value="" />  <div id="message"></div>  <input type="button" οnclick="helloMessage()" />

 </body></html>

相关资源:dwr配置文件和注解两种方式的两个demo
转载请注明原文地址: https://www.6miu.com/read-5047552.html

最新回复(0)