如何在GWT中沟通两个模块 [英] How to communicate two modules in GWT

查看:111
本文介绍了如何在GWT中沟通两个模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gwt应用程序中创建了两个模块名称module1和module2。我想在几秒钟后同时将消息从module1传递到module2,将module2传递给module1。
我写了下面的代码,但它给了我错误,无法在classpath中找到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);
}
});


$ b 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帮助我。


解决方案

如果您尝试使用两个分离的模块.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){
//对收到的消息进行处理(创建事件等)
返回Hello+消息;
}
私有本地静态exportMyJavaMethod()/ * - {
$ wnd.myJavaMethod = @ my.package.module1.MyClass1 :: myJavaMethod;
} - * /;
}


package my.package.module2;
public class MyClass2 implements EntryPoint {
public void onModuleLoad(){
String ret = callMyJavaMethod(foo);
}
私有本地静态callMyJavaMethod(String s)/ * - {
return $ wnd.myJavaMethod(s);
} - * /;
}

请注意,使用JSNI,您必须根据基本类型传递消息(请参阅文档

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


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.

解决方案

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.

这篇关于如何在GWT中沟通两个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆