GWTP ViewWithUiHandlers:未设置 uiHandlers。您是否从视图的构造函数中调用了 getUiHandlers()?在 PresenterWidget 中

GWTP ViewWithUiHandlers: uiHandlers are not set. Did you call getUiHandlers() from your view's constructor? in PresenterWidget

我有一个我不明白的问题。我想我已经做了我应该做的,但它不起作用。

我无法使用 ui 处理程序从视图中执行演示器方法。错误说我没有设置 ui 处理程序,但我设置了:

@Inject
FileUploaderPresenter(EventBus eventBus, MyView view, PlaceManager placeManager) {
   super(eventBus, view);
   getView().setUiHandlers(this);
   this.placeManager = placeManager;
}

public FileUploaderView() {
   super();
   this.getUiHandlers();
   this.initWidget(uploader);
}

但是我得到一个错误:

com.gwtplatform.mvp.client.ViewWithUiHandlers
SEVERE: uiHandlers are not set.  Did you call getUiHandlers() from your view's constructor?

我肯定会 post 我所有的 类 相关:

public class FileUploaderModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
        bindSingletonPresenterWidget(FileUploaderPresenter.class, FileUploaderPresenter.MyView.class, FileUploaderView.class);
    }
}

public class FileUploaderPresenter extends PresenterWidget<FileUploaderPresenter.MyView> implements FileUploaderUiHandlers {
    interface MyView extends View, HasUiHandlers<FileUploaderUiHandlers> {
    }

    PlaceManager placeManager;

    @Inject
    FileUploaderPresenter(EventBus eventBus, MyView view, PlaceManager placeManager) {
    super(eventBus, view);
        getView().setUiHandlers(this);
    this.placeManager = placeManager;
    }

    @Override
    protected void onBind() {
    super.onBind();
    }

    @Override
    public void onOk() {
    Routing.Redirect.toLoginPage(placeManager);
    }

    @Override
    public void onUnauthorized() {
    Routing.Redirect.toLoginPage(placeManager);
    }

}

interface FileUploaderUiHandlers extends UiHandlers {
    void onUnauthorized();

    void onOk();

}

public class FileUploaderView extends ViewWithUiHandlers<FileUploaderUiHandlers> implements FileUploaderPresenter.MyView {

    MaterialFileUploader uploader = new MaterialFileUploader();

    public FileUploaderView() {
        super();
        this.getUiHandlers();
        this.initWidget(uploader);
        MaterialUploadLabel label = new MaterialUploadLabel();
        label.setTitle("Put Your's files here and here");
        label.setDescription("Some description here");
        uploader.add(label);
        uploader.setMaxFileSize(10000000);
        uploader.setUrl(ServiceRouting.FULL_SERVICE +           ServiceRouting.FileService.upload);
    addHandlers();
    }

    private void addHandlers() {
    uploader.addUnauthorizedHandler(new UnauthorizedHandler<UploadFile>() {
        @Override
        public void onUnauthorized(UnauthorizedEvent<UploadFile> event) {
        GWT.log("UnauthorizedEvent (" + event.getTarget().getName() + " | " + event.getResponse().getCode() + " | " + event.getResponse().getMessage() + "|" + event.getResponse().getBody()
            + ")");
        MaterialToast.fireToast("Redirect to login page");
        getUiHandlers().onUnauthorized();
        }
    });
}

我将这个小部件实例化为:FileUploaderView()

是否会发生此错误,因为我没有使用 uiBinder? 这是我的第一个 PresenterWidget。也许它应该与标准 Presenter 不同。任何帮助表示赞赏。

好的,让我们从您的代码中跟踪步骤:

这是您的演示者的构造函数。 MyView view 已在此处创建注入以传递给您的构造函数。

 @Inject
FileUploaderPresenter(EventBus eventBus, MyView view, PlaceManager placeManager) {
   super(eventBus, view);
   getView().setUiHandlers(this)

这意味着

public FileUploaderView() {
   super();
   this.getUiHandlers();
   this.initWidget(uploader);
}

在演示者构造函数之前被调用。因此,在 Presenter 的构造函数中设置它们之前,您先调用 this.getUiHandlers() 。

将调用移至某种@UiHandler

好的,我自己想出来了。

问题是这 Presenter/View 对是一个小部件。所以它应该用gwtp的slot机制实例化。为此,我们使用带有 Presenter class 参数的绑定方法(不像我那样查看)。

@Inject UploaderPresenter fileUploaderPresenter;

@Override
protected void onBind() {
    super.onBind();
    setInSlot(SLOT_FILE_UPLOAD, fileUploaderPresenter);
}

这是文档:

http://dev.arcbees.com/gwtp/tutorials/tutorial-part2.html

栏目:

  • 绑定 PresenterWidget
  • 设置在槽位
  • 绑定插槽

http://dev.arcbees.com/gwtp/core/presenters/