简体   繁体   中英

GWT mvp sample project: cannot call method of presenter

I am using the GWT mvp sample project to create my own mvp application.

I pretty much did what they did, ie defined a Presenter interface and then different presenter classes.

In their code, they are doing something like this in one of the View classes:

@UiHandler("loginButton")
void onClick(ClickEvent e) {
    if (presenter != null) {
        presenter.onLoginButtonClicked();
    }
}

The presenter is injected through this method:

public void setPresenter(IPresenter presenter) {
    this.presenter = presenter;

}

Well duh... turns out, I am unable to call the onLoginButtonClicked, since IPresenter is an interface. They do that in their code. How is this supposed to work?

You have to have a class that implements Presenter interface for this view.

Something like:

public class MyActivity extends AbstractActivity implements MyView.Presenter {}

Then you have a View class:

public interface MyView extends IsWidget {

    public interface Presenter {
        void onLoginButtonClicked();
    }

    void setPresenter(Presenter listener);
}

Finally, you will have an implementation of this view:

public class MyViewImpl extends Composite implements MyView {}

NB: I strongly recommend Activities and Places pattern. It gives a good structure for any app with more than one view, and adds good history support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM