您的位置:首页 > 编程语言 > Java开发

DWR: 创建与JAVA对象对应的JS对象。

2009-02-17 10:57 567 查看
使用DWR,可以非常方便的将一个JS对象转变成一个JAVA对象,提供数据给后台进行处理。

下面是的官方网站例子:

Creating Javascript objects to match Java objects

Suppose you have an exposed Java method like this:

public class Remote {
public void setPerson(Person p) {
this.person = p;
}
}

And
Person
looks like this:

public Person {
private String name;
private int age;
private Date[] appointments;
// getters and setters ...
}

Then you can call this from Javascript like this:

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: