0

我正在尝试将 GWT 视图与其表示层绑定,但它似乎没有做任何事情。

这是一个 Spring Roo GWT 生成的项目,我正在尝试尽可能使用给定的脚手架。视图是一个简单的按钮 (R.ui.xml),视图的其余部分在 R.java 中定义:

public class R extends Composite implements RPresenter.Display {

    interface MyUiBinder extends UiBinder<Widget, R> {}
    private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

    @UiField Button myButton;

    private ClickHandler buttonClickHandler = null;

    public R(){
        initWidget(uiBinder.createAndBindUi(this));
    }

    @UiHandler("myButton")
    void onButtonClick(ClickEvent event){
        GWT.log('Button clicked');
        if (buttonClickHandler != null){    
            GWT.log("buttonClickHandler event triggered");
            buttonClickHandler.onClick(event);
        }
    }

    @Override
    public void setButtonClickHandler(ClickHandler buttonClickHandler) {

        GWT.log("setButtonClickHandler");
        this.buttonClickHandler = buttonClickHandler;
    }

}

主持人:

public class RPresenter {

    public interface Display extends IsWidget {
        void setButtonClickHandler(ClickHandler buttonClickHandler);
    }

    private final Display display;
    private final EventBus eventBus;

    @Inject
    public RPresenter(EventBus eventBus, Display display){
        this.display = display;
        this.eventBus = eventBus;
        bind();
    }

    private void bind(){
        display.setButtonClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                GWT.log("onClick event triggered");
            }
        });
    }

    public void go(HasWidgets container){
        container.add(display.asWidget());
    }

}

对于我的 GIN 模块,我使用 ...client.scaffold.ioc 包中生成的 ScaffoldModule :

public class ScaffoldModule extends AbstractGinModule {

    @Override
    protected void configure() {

        GWT.log("ScaffoldModule configure");

        bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
        bind(ApplicationRequestFactory.class).toProvider(RequestFactoryProvider.class).in(Singleton.class);
        bind(PlaceController.class).toProvider(PlaceControllerProvider.class).in(Singleton.class);

        //bind(RPresenter.Display.class).to(R.class).in(Singleton.class);
        bind(RPresenter.Display.class).to(R.class);
    }

    static class PlaceControllerProvider implements Provider<PlaceController> {

        private final EventBus eventBus;

        @Inject
        public PlaceControllerProvider(EventBus eventBus) {
            this.eventBus = eventBus;
        }

        public PlaceController get() {
            return new PlaceController(eventBus);
        }
    }

    static class RequestFactoryProvider implements Provider<ApplicationRequestFactory> {

        private final EventBus eventBus;

        @Inject
        public RequestFactoryProvider(EventBus eventBus) {
            this.eventBus = eventBus;
        }

        public ApplicationRequestFactory get() {
            ApplicationRequestFactory requestFactory = GWT.create(ApplicationRequestFactory.class);
            requestFactory.initialize(eventBus);
            return requestFactory;
        }
    }
}

在 GWT 开发模式控制台中,“ScaffoldModule 配置”永远不会显示,但生成的脚手架似乎绑定得很好,因为事件可以顺利地从组件传递到组件,除非绑定神奇地发生在其他地方并且已经死了代码。

当我将 bind(RPresenter.Display.class).to(R.class) 放入时,它似乎没有进行绑定。我在 GWT 控制台中得到的唯一输出是“Button clicked”,它在视图中被调用,然后就没有了。我显然错过了一些东西,有什么想法吗?

4

1 回答 1

1

对 GWT.log() 的调用不会从 AbstractGinModule 输出任何内容 - 扩展 AbstractGinModule 的类(在您的情况下为 ScaffoldModule)由 gin在编译时使用来决定将哪些具体实现用于注入接口。从您的其余描述(即 UI 显示在应用程序中)看来,您的依赖注入工作正常。

于 2011-01-13T22:12:16.003 回答