【问题标题】:GWT result state changes after dispatch分派后 GWT 结果状态更改
【发布时间】:2018-09-21 12:57:51
【问题描述】:

当数据项从服务器通过调度程序返回到 Presenter(客户端)时,数据项布尔标志不会保持其状态。

共享包

 public class ResourceItem extends BaseResourceItem implements IsSerializable {

  private String name;

  public ResourceItem() {
    super();
  }

  public ResourceItem(String name) {
    super(true);
    this.name = name;
  } 
}

public class BaseResourceItem {

  private boolean removeEnabled = true;

  public BaseResourceItem() {
    super();
  }

  public BaseResourceItem(boolean removeEnabled) {
    super();
    this.removeEnabled = removeEnabled;
  }

  public boolean isRemoveEnabled() {
    return removeEnabled;
  }

  public void setRemoveEnabled(boolean removeEnabled) {
    this.removeEnabled = removeEnabled;
  }
}

有问题的标志是 removeEnabled 。默认情况下它是 true,即使我在服务器端将它设置为 false,当 Presenter 获取它时,由于某种原因它被设置为 false。我错过了序列化的东西吗? (此时想不出别的)。

服务器包

@GenDispatch
public class GetModelSettings {

    @Out(1)
    List<ResourceItem> listOfSettings;
}

public class GetModelSettingsHandler implements ActionHandler<GetModelSettingsAction, GetModelSettingsResult> {

        @Override
        public GetModelSettingsResult execute(GetModelSettingsAction action, ExecutionContext context)
                throws ActionException {

            ResourceItem item1 = new ResourceItem();
            ResourceItem item2 = new ResourceItem();
            item2.setRemoveEnabled(false);

            list.add(item1);
            list.add(item2);

            // item1 -> true
            // item2 -> false

            return new GetModelSettingsResult(list);
        }
    }

如您所见,一个简单的处理程序返回一个列表。至此,数据是正确的,一项设置​​为真,另一项设置为假。

客户端包

public class ModelSettingsPresenter {

    dispatcher.execute(new GetModelSettingsAction(), new AsyncCallback<GetModelSettingsResult>() {

        @Override
        public void onSuccess(GetModelSettingsResult result) {
            itemList = result.getListOfSettings(); 
            // itemList.get(0) -> true
            // itemList.get(1) -> true
        }
    });
}

在此演示器中,数据项的标志都设置为 true。任何想法为什么会发生这种情况?

【问题讨论】:

    标签: gwt gwtp


    【解决方案1】:

    这与用于继承的序列化有关。

    在反序列化期间,不可序列化类的字段将使用类的公共或受保护的无参数构造函数进行初始化。可序列化的子类必须可以访问无参数构造函数。可序列化子类的字段将从流中恢复。

    更多信息可以在不同的线程Java object Serialization and inheritance中找到

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 2020-09-21
      • 1970-01-01
      • 2020-04-22
      • 2021-12-10
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多