DWR注解
?
一、配置DWR使用注解功能
如果让dwr使用注解功能需要在web.xml进行如下配置:
这样设置之后就可以在dwr中使用注解了,非常的方便。
二、DWR提供的注解类型
经常用到的注解主要有:@RemoteProxy、@RemoteMethod、@DataTransferObject和@RemoteProperty。
1. @RemoteProxy和@RemoteMethod
@RemoteMethod对应于原来dwr.xml文件中的create标签,用于创建DWR所提供的远程方法;而@RemoteMethod对应于create标签中的 <include method=”"/>,用来指定所要暴露的方法名称。我们举例来说明:
如果使用Spring中的DAO或逻辑层则需要进行如下的设置:
2. @DataTransferObject和@RemoteProperty
@DataTransferObject对应于原来dwr.xml文件中的convert标签,用于转换Java对象;@RemoteProperty则对应于convert标签中的 <param name=”include” value=”" />。
举例说明一下:
关于具体每个注释使用的方法已经所包含的参数可以参考Java Doc。使用DWR2.0的注解极大的简化了原来dwr.xml的配置,非常的方便。
?
总结:
?针对DWR对Javabean的注解分为两类:
Object和Class
针对Class的注解使用:
@RemoteProperty注解类为远程访问类,相等于create
@RemoteMethod 注解类为远程访问方法,相等于dwr.xml中白名单中注解的方法
注意默认的类中所有的方法为全部在黑名单中。不可作为远程访问对象的。
?
针对:Object使用@DataTransferObject 和@RemoteProperty注解接口
@DataTransferObject仅仅注解类对象
@RemoteProperty注解属性和方法的应用
?
?
?
使用远程访问类的
package com.dwr.test;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
/**
?* 注解此类为远程访问对象的应用
?* @author Rabby
?*
?*/
@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>
????com.dwr.test.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" onclick="helloMessage()" />
?</body>
</html>
?
?
?
http://www.open-lib.com/
very good!