简体   繁体   English

GWT托管模式和applet

[英]GWT hosted mode and applet

It's possible to communicate between applet and GWT application (and vice-versa) when launching GWT in hosted mode? 在托管模式下启动GWT时,可以在applet和GWT应用程序之间进行通信(反之亦然)? I think no, because applet can communicate only with JavaScript (through LiveConnect), but GWT's hosted mode don't produce any JS... 我认为不行,因为applet只能与JavaScript通信(通过LiveConnect),但GWT的托管模式不会生成任何JS ...

Although hosted-mode runs part of the code in the jvm, all jsni/dom/native calls are delegated to the browser, so in your case should not be any difference, and you can call the js to communicate to your applet. 虽然托管模式在jvm中运行代码的一部分,但所有jsni / dom / native调用都被委托给浏览器,因此在你的情况下应该没有任何区别,你可以调用js与你的applet进行通信。 Of course, what it is not possible is to debug gwt and applet code in the same debugger session. 当然,不可能的是在同一个调试器会话中调试gwt和applet代码。

To call applet methods from GWT, you need a jsni block like: 要从GWT调用applet方法,您需要一个jsni块,如:

 private native Object callFoo(String param) /*-{
    var appplet = document.getElementById('myapplet');
    return applet.foo(param);
 }-*/

And to call GWT code from your applet, you need another jsni method to export your gwt code (note that you have to call it once in your entry-point) 要从您的applet调用GWT代码,您需要另一个jsni方法来导出您的gwt代码(请注意,您必须在入口点调用一次)

 private native void exportBar() /*-{
    window.bar = function(param) {
      return @com.examle.MyClass::myStaticMethod(*)(param);
    }
 }-*/

Anyway, the best approach I know to communicate with applets is using gwtai because all the boiler-plate code needed (wrappers and jsni) is generated automatically. 无论如何,我知道与applet通信的最佳方法是使用gwtai,因为所需的所有样板代码(包装器和jsni)都是自动生成的。

Gwtai, works in hosted mode without problems and although they say in their site that don't expect it to be stable , It is stable enough and I have used it for a long in production. Gwtai,在托管模式下工作没有问题,虽然他们在他们的网站上don't expect it to be stable ,它足够稳定,我已经使用它长期生产。

GwtAI provides easy to use cross-browser Java Applets integration to Google Web Toolkit (GWT) projects. GwtAI提供易于使用的跨浏览器Java Applet与Google Web Toolkit(GWT)项目的集成。 GwtAI contains a number of utilities and helper classes, such as automatic creation of a wrapper widget and mechanism to communicate with Java Applets. GwtAI包含许多实用程序和帮助程序类,例如自动创建包装器窗口小部件和与Java Applet通信的机制。

You need to download two files (GwtAI-Client.jar and GwtAI-Core.jar ) and include in the classpath of your project, then modify your .ui.xml file to inherit gwtai, and you will be able to start coding. 您需要下载两个文件(GwtAI-Client.jar和GwtAI-Core.jar)并包含在项目的类路径中,然后修改.ui.xml文件以继承gwtai,您就可以开始编码了。

First you have to define the applet interface in your gwt-code, so as GWT compiler using deferred binding creates the comunication class implementation. 首先,您必须在gwt-code中定义applet接口,以便使用延迟绑定的GWT编译器创建comunication类实现。

 @ImplementingClass(MyClassImpl.class)
 @Height("60") @Width("350") @Archive("GwtAI-Client.jar, MyApp.jar")
 public interface MyApplet extends Applet {
   public Object foo();
 }

Then you have to implement this interface in your applet code. 然后,您必须在applet代码中实现此接口。

 public class MyClassImpl extends JApplet implements MyApplet {
   public Object foo(){
      return "Hello";
   }
 }

Finally use the applet from your gwt app 最后使用你的gwt应用程序中的applet

 MyApplet applet = GWT.create(MyApplet.class);
 Object foo = applet.foo();

Read the GettingStarted guide for more info. 阅读GettingStarted指南以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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