GWT 结果状态在发送后发生变化

GWT result state changes after dispatch

当项从服务器通过调度程序 return 发送到演示者(客户端)时,数据项布尔标志将不会保持其状态。

共享包

 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);
        }
    }

如您所见,一个简单的处理程序 return 一个列表。此时,数据是正确的,一项的标志设置为真,另一项设置为假。

客户端包

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。知道为什么会这样吗?

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

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

More on it can be found in different thread Java object Serialization and inheritance