sidebar 隐藏/显示
2007-09-18

这东西-DWR - [learning]

今天,PM安排我学习DWR,为了是后面工作我能帮上忙!边看边翻译了下大体内容。错在所难免!

原文出自:http://getahead.org/dwr/getstarted/ 

DWR: Easy Ajax for Java

DWR allows JavaScript in a browser to interact with Java on a server.

1.初识DWR

1.1 下载dwr.jar,并放置到WEB-INF/lib文件夹下。

1.2 编辑配置文件,以下代码加入WEB-INF/web.xml文件,分别对应<servlet>部分和

<servlet-mapping> 部分.

<servlet>

  <servlet-name>dwr-invoker</servlet-name>

  <display-name>DWR Servlet</display-name>

  <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>

  <init-param>

     <param-name>debug</param-name>

     <param-value>true</param-value>

  </init-param>

</servlet>

 

<servlet-mapping>

  <servlet-name>dwr-invoker</servlet-name>

  <url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

如果用DWR2.xservlet-class最好用:org.directwebremoting.servlet.DwrServlet。好版本同样可行,但建议用新的。

接着,创建dwr.xml文件,放在同web.xml文件相同的目录,例如:

<!DOCTYPE dwr PUBLIC

    "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"

    "http://www.getahead.ltd.uk/dwr/dwr10.dtd">

<dwr>

  <allow>

    <create creator="new" javascript="JDate">

      <param name="class" value="java.util.Date"/>

    </create>

    <create creator="new" javascript="Demo">

      <param name="class" value="your.java.Bean"/>

    </create>

  </allow>

</dwr>

配置文件定义了什么类可以让DWR通过JavaScript创建实例和远程访问。如上例所示,定义了两个类允许在JavaScript中通过类名访问。New creator的类如同所有JavaBeang一样都是有公共的无参构造函数。值得注意记住DWR对于New creator类的一些限制:

1)避免使用JavaScript保留字;(2)避免重载方法。

1.3 进入如下地址看看

http://localhost:8080/[YOUR-WEBAPP]/dwr/

 

2.Scripting Introduction

    最大的挑战是创建远程接口通过Ajax去匹配Java代码(Ajax的异步天性)。DWR通过引入callback函数解决这个问题,当数据从服务器端返回时,callback函数被调用。有两种推荐的方式去调用callback函数,一种是增加callback函数到参数列表,另一种是新增调用元数据对象。也可以把callback函数放在初始华的参数列表上,但不建议这么做(backwards compatibility

2.1简单的Callback函数

假设有如下的Java方法:

Public class Remote{

       Public String getData(int index){……}

}

    我们可以用如下形式的JavaScript代码:

<script type="text/javascript"

    src="[WEBAPP]/dwr/interface/Remote.js"> </script>

<script type="text/javascript"

    src="[WEBAPP]/dwr/engine.js"> </script>

...

function handleGetData(str) {

  alert(str);

}

 

Remote.getData(42, handleGetData);

'42'Java函数getData()的参数。

同样,我们也可以用一行代码的形式:

Remote.getData(42, function(str) { alert(str); });

 

2.2调用元数据对象

利用元数据对象指定callback函数和其他可选参数,例如:

Remote.getData(42, {

  callback:function(str) { alert(str); }

});

这种方法有一定的优势:不但可以依你自己的风格,而且更重要的是它允许你指定调用选项。

 

2.3 设置超时和错误处理(Timeouts and Handling Errors

我们也可以指定超时时间和错误处理。例如:

Remote.getData(42, {

  callback:function(str) { alert(str); },

  timeout:5000,

  errorHandler:function(message) { alert("Oops: " + message); }

});

 

2.3 获得callback函数(Finding the callback method

There are some instances when it might not be tricky to distinguish between the various callback placement options. (Remember that Javascript does not support overloaded functions). For example:

Remote.method({ timeout:3 }, { errorHandler:somefunc });

One of the 2 parameters is a bean parameter and the other is a call meta-data object but we have no way of telling which. In a cross-browser world we have to assume that null == undefined. So currently, the rules are:

l         If there is a function first or last then this is the callback function, there is no call meta-data object and the other parameters are the Java args.

l         Otherwise, if the last param is an object with a callback member that is a function then this is call meta-data the other params are Java args.

l         Otherwise, if the first parameter is null we assume that there is no callback function and the remaining params are Java args. HOWEVER we check to see that the last param is not null and give a warning if it is.

l         Finally if the last param is null, then there is no callback function.

l         Otherwise, this is a badly formatted request - signal an error.

 

 

2.4 Creating Javascript objects to match Java objects

假设我们有如下的Java代码:

public class Remote {

  public void setPerson(Person p) {

    this.person = p;

  }

}

Person的代码如下:

public class Person {//应该是作者漏了关键字class,怎么这么不小心呢!

  private String name;

  private int age;

  private Date[] appointments;

  // getters and setters ...

}

接着我们就可以通过如下JavaScript代码调用:

var p = {

  name:"Fred Bloggs",

  age:42,

  appointments:[ new Date(), new Date("1 Jan 2008") ]

};

Remote.setPerson(p);

Any fields missing from the Javascript representation will be left unset in the Java version.

Since the setter returned 'void' we did not need to use a callback and could just leave it out. If you want to be notified of the completion of a server-side method that returns void then you can include a callback method. Obviously DWR will not pass any data to it.





评论

  • 黑色看的太压抑~

    hoohbood () 发表于 2009-07-29 16:51:25
  • thank you ...你写很好...

    li () 发表于 2008-03-05 13:31:01

发表评论

 姓名:
 E-mail:
 地址: