【问题标题】:How to edit value of ValueProxy of gwt requestfactory at client side?如何在客户端编辑 gwt requestfactory 的 ValueProxy 的值?
【发布时间】:2011-09-17 22:33:43
【问题描述】:

我有 2 个模型:ContactGroup 和 Contact。 ContactGroup 包含许多联系人。

在页面中,我必须像这样显示通信组中的组列表和联系人数量:

  • Foo 组(12 个联系人)
  • 群组栏(20 个联系人)

所以我在服务器端使用了 DTO ContactGroupInfo:

public class ContactGroupInfo {
    private Integer contactCount;
    private Long id;
    private String name;

    public Integer getContactCount() { return this.contactCount; }
    public Long getId() { return this.id; }
    public String getName() { return this.name; }
    public void setContactCount(Integer count) { this.contactCount = count; }
    public void setId(Long id) { this.id = id; }
    public void setName(String name) { this.name = name; }
}

在此 ContactGroupInfo 中,我添加了 contactCount 字段,该字段不是 ContactGroup 实体中的字段

在客户端,我使用了 ValueProxy:

@ProxyFor(value = ContactGroupInfo.class, locator = ContactGroupService.class)
public interface LightContactGroupProxy extends ValueProxy {
    Integer getContactCount();
    Long getId();
    String getName();
    void setContactCount(Integer count);
    void setId(Long id);
    void setName(String name);
}

因此,当服务器端向客户端返回 LightContactGroupProxy 列表时,我将该列表 a 存储在 ArrayList 中以呈现给 CellTable。

这就是我遇到的问题:当我需要在客户端编辑组的名称时,我无法直接编辑 LightContactGroupProxy 对象。

  • 所以我必须将新名称发送到服务器以返回具有新名称的新 LightContactGroupProxy。这无效,因为我必须重新计算联系人(虽然我知道联系人的数量不会改变)。
  • 或者我必须将联系人数量和新名称都发送到服务器,以使用新名称创建一个新的 LightContactGroupProxy。这不是我想要的,因为如果 LightContactGroupProxy 有很多其他字段,我必须发送很多字段。

我不知道 GWT 团队为什么要设计不可变代理。所以拜托,有人有requestfactory的经验,请告诉我处理从服务器返回的ValueProxy的正确方法,以便我们可以使用它们来渲染和编辑?

谢谢

【问题讨论】:

    标签: gwt requestfactory


    【解决方案1】:

    也许你应该尝试这样的事情:

    ContactGroupContext ctx = requestFactory.newContactGroupContext();
    LightContactGroupProxy editableProxy = ctx.edit(lightContactGroupProxy);
    editableProxy.setName(newName);
    ctx.saveInfoAndReturn(editableProxy).fire(receiver); // or just ctx.fire();
    

    无论如何,在这种情况下我不会使用ValueProxy,我会直接获取具有瞬态属性contactCountContactGroup 实体。如果您不希望每次请求 ContactGroup 时都计算该属性,则该属性可以是原语或 ValueProxy

    【讨论】:

    • 我不能使用 ctx.edit(lightContactGroupProxy ) 或使用 LightContactGroupProxy 作为方法的参数,因为我添加了 contactCount 字段。此字段不存在 ContactGroup 实体。所以服务器总是会在 ReflectiveServiceLayer.setProperty() 抛出异常“Could not locate setter for property contactCount in type ContractGroup”
    • 当您尝试在 ContactGroup 上设置 contactCount 时会抛出此异常。你想要的是在 ContactGroupInfo 上设置 contactCount。您可能在 LightContactGroupProxy 上的 @ProxyFor 注释中遇到问题。确保你有value = ContactGroupInfo.class,然后放下定位器,ValueProxy 不需要它。
    • 谢谢你,米凯尔。我再试一次,发现我可以使用 ValueProxy 作为方法的参数。我检查了我的代码历史记录,发现我为@ProxyFor(到 ContactGroup.class)设置了错误的值。所以我想我会使用编辑的ValueProxy作为RequestFactory方法的参数,服务器会编辑编辑的ValueProxy并将其返回给客户端。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多