DWR根据dwr.xml生成和Java代码类似的Javascript代码。
相对而言Java同步调用,创建与Java代码匹配的Ajax远程调用接口的最大挑战来至与实现Ajax的异步调用特性。
DWR通过引入回调函数来解决这个问题,当结果被返回时,DWR会调用这个函数。
有两种推荐的方式来使用DWR实现远程方法调用。可以通过把回调函数放在参数列表里,也可以把回调函数放到元数据对象里。
当然也可以把回调函数做为第一个参数,但是不建议使用这种方法。因为这种方法在处理自动处理http对象时(查看"Alternative Method")上会有问题。这个方法主要是为向下兼容而存在的。
假设你有一个这样的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); });
另外一种语法时使用"调用元数据对象"来指定回调函数和其他的选项。上面的例子可以写成这样:
Remote.getData(42, { callback:function(str) { alert(str); } });
这种方法有很多优点:易于阅读,更重要的指定额外的调用选项。
在回调函数的元数据中你可以指定超时和错误的处理方式。例如:
Remote.getData(42, {
callback:function(str) { alert(str); },
timeout:5000,
errorHandler:function(message) { alert("Oops: " + message); }
});
有些情况下我们很难区分各种回调选项(记住,Javascript是不支持函数重载的)。例如:
Remote.method({ timeout:3 }, { errorHandler:somefunc });
这两个参数之一是bean的参数,另一个是元数据对象,但是我们不能清楚的告诉DWR哪个是哪个。为了可以跨浏览器,我们假定null == undefined。 所以当前的情况,规则是:
假设你有这样的Java方法:
public class Remote { public void setPerson(Person p) { this.person = p; } }
Person对象的结构是这样的:
public Person { 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);
在Javascript没有出现的字段,在Java中就不会被设置。
因为setter都是返回'void',我们就不需要使用callback函数了。如果你想要一个返回void的服务端方法的完整版,你也可以加上callback函数。很明显DWR不会向它传递任何参数。
<!-- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"> <rdf:Description rdf:about="http://wiki.javascud.org/display/dwrcn/Scripting+Introduction" dc:identifier="http://wiki.javascud.org/display/dwrcn/Scripting+Introduction" dc:title="Scripting Introduction" trackback:ping="http://wiki.javascud.org/rpc/trackback/5012" /> </rdf:RDF> --><!-- Root decorator: all decisions about how a page is to be decorated via the inline decoration begins here. --><!-- Switch based upon the context. However, for now, just delegate to a decorator identified directly by the context. -->