【问题标题】:How to communicate two modules in GWT如何在 GWT 中通信两个模块
【发布时间】:2013-06-24 10:45:19
【问题描述】:

我在 gwt 应用程序中创建了两个模块名称 module1 和 module2。我想在几秒钟后同时将消息从模块 1 传递到模块 2 和模块 2 到模块 1。 我编写了以下代码,但它给了我错误,无法在类路径中找到 module1.gwt.xml。

        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.

【问题讨论】:

    标签: java gwt jsni gwtquery gwt-exporter


    【解决方案1】:

    如果您试图在同一个网页中包含两个独立的模块(*.nocache.js 文件),除非您使用 JS,否则您无法传递消息。

    使用 JSNI 从 module1 导出一些方法,以便在 javascript 中可用,然后也使用 JSNI 从 module2 调用该方法。

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

    请注意,使用 JSNI,您必须根据原始类型传递消息(请参阅documentation

    顺便说一句:我宁愿使用 gwtexporter 来导出我希望在 JS 中可用的方法和类,并使用 gwtquery 来调用 JS 方法而不是使用 JSNI。

    【讨论】:

      【解决方案2】:

      您的应用程序只能有一个入口点,但您可以让您的主模块继承多个其他 gwt 应用程序。我建议研究模块继承。您可以在 .gwt.xml 文件中继承一个模块,该模块将被加载,并自动调用其 onModuleLoad 方法。

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

      【讨论】:

      • Your app can only have one entry point 并不完全正确,您可以在应用程序中拥有多个入口点。
      • 我的立场是正确的,在重新查看文档后,确实可以使用多个入口点。
      猜你喜欢
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多