How to communicate two modules in GWT

强颜欢笑 提交于 2019-12-12 04:58:07

问题


I have created two module name module1 and module2 in gwt application. I want to pass message from module1 to module2 and module2 to module1 simultaneously after some seconds. I write the following code, but it gives me error unable to find module1.gwt.xml in classpath.

        public void onModuleLoad() {
                mainBus.fireEvent(new PingEvent("-----Simulation Started-----"));
        }

        module1

        public void onModuleLoad() 
            {
                GWTEventBus.mainBus.addHandler(PingEvent.TYPE, new PingEventHandler(){
                    public void onEvent(PingEvent event) {
                        System.out.print("Inside Ping --> ");
                        new Timer(){
                            public void run() {
                                GWTEventBus.mainBus.fireEvent(new PongEvent("Pong fired..."));
                            }
                        }.schedule(1000);
                    }
                });


            }
        module2
        public void onModuleLoad() 
            {
                //final SimpleEventBus mainBus = new SimpleEventBus();
                GWTEventBus.mainBus.addHandler(PongEvent.TYPE, new PongEventHandler(){
                    public void onEvent(PongEvent event) {
                        System.out.print("Inside Pong1 --> ");
                        new Timer(){
                            public void run() {
                                GWTEventBus.mainBus.fireEvent(new PingEvent("Ping fired..."));
                            }
                        }.schedule(1000);
                    }
                });


            }

    plz help me.

回答1:


If you are trying to have two separated modules (*.nocache.js files) included in the same webpage, you can not pass messages unless you use JS.

Use JSNI to export some method from module1 so as it is available in javascript, then call this method from module2 using JSNI as well.

package my.package.module1;
public class MyClass1 implements EntryPoint {
  public void onModuleLoad() {
    exportMyJavaMethod();
  }
  public static String myJavaMethod(String message) {
    // Do whatever with the message received (create an event, etc.)
    return "Hello " + message;
  }
  private native static exportMyJavaMethod() /*-{
    $wnd.myJavaMethod = @my.package.module1.MyClass1::myJavaMethod;
  }-*/;
}


package my.package.module2;
public class MyClass2 implements EntryPoint {
  public void onModuleLoad() {
    String ret = callMyJavaMethod("foo");
  }
  private native static callMyJavaMethod(String s) /*-{
    return $wnd.myJavaMethod(s);
  }-*/;
}

Note that using JSNI, you have to pass messages based on primitive types (see documentation)

BTW: I'd rather use gwtexporter to export methods and classes I want available in JS, and gwtquery to call JS methods instead of using JSNI.




回答2:


Your app can only have one entry point, but you can have your main module inherit multiple other gwt apps. I would suggest looking into module inheritance. You can inherit a module in your .gwt.xml file, and that module will be loaded, and its onModuleLoad method will be called automatically.

https://developers.google.com/web-toolkit/doc/latest/DevGuideOrganizingProjects#DevGuideModules



来源:https://stackoverflow.com/questions/17273572/how-to-communicate-two-modules-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!