当前位置: 首页 > 工具软件 > Drv.js > 使用案例 >

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

慕学海
2023-12-01

使用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.

 类似资料: