java - GWT 射频 : How to share the same code in client and server

标签 java gwt javabeans requestfactory

我想使用相同的代码在客户端和服务器端对对象进行排序和操作。

但我遇到了一个问题,因为在客户端中我们需要一个代表服务器类的代理接口(interface)。

有没有办法在两者中使用相同的接口(interface)?我知道 RF 有一种机制可以在通过网络发送时将 bean 属性从服务器实例复制到客户端实例。

最佳答案

使用相同 API 的一种方法是使用代理扩展和域对象实现的接口(interface)。

// common interfaces
interface Foo { … }
interface Bar<T extends Foo> {
  int getX();
  void setX(int x);

  // setters need to use generics
  List<T> getFoos();
  void setFoos(List<T> foos);

  // with only a getter, things get easier:
  Bar getParent();
}

// domain objects
class RealFoo implements Foo { … }
class RealBar implements Bar<RealFoo> {
  int x;
  List<RealFoo> foos;
  RealBar parent;

  @Override
  public RealBar getParent() { return parent; }

  // other getters and setters
}

// proxy interfaces
@ProxyFor(RealFoo.class)
interface FooProxy extends Foo { … }

@ProxyFor(RealBar.class)
interface BarProxy extends Bar<FooProxy> {
  @Override
  BarProxy getParent();

  // other getters and setters
}

然后您可以在客户端和服务器端使用 Comparator<Foo>Comparator<Bar>

我通常只实现 traits(aspectsfacets,按你喜欢的方式调用它们)虽然( HasIdHasLabelHasPosition等),而不是完整的域对象的 API。然后我可以使用 HasId 获取任何对象的键以将它们放入 map 或比较是否相等,HasLabel 用于显示(客户端自定义 Cell,服务器端发送到客户端的错误消息,等), HasPosition 用于排序等

关于java - GWT 射频 : How to share the same code in client and server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15849602/

相关文章:

java - Modelio 类图隔间

java - 为Web应用程序设置指定的系统属性

java - Eclipse 氧气源水平变化

sorting - GWT 数据网格滚动到排序的最后一列

Java Entity Bean 已定义但作为类对象传递

java - 在jsp中显示迭代bean值

java - 在没有 BeanNameAware 接口(interface)的情况下将 bean id 记录到 log4j 日志文件中

java - 当我第二次按下按钮时,应用程序停止工作

java - 在底部的向下箭头上向 jTable 添加一行

java - GWT "database"(客户端)