如何在 GWT 中公开类功能 [英] How to expose class functionality in GWT
问题描述
我有一个用 Java 编写的类库,想将其转换为 Javascript.所有方法都非常简单,主要与操作集合有关.我有一个类,GameControl,我可以实例化它,我希望它的方法暴露给页面上的其他 Javascript 代码.
I have a class library written in Java and want to convert it to Javascript. All methods are pretty simple and mostly have to do with manipulating collections. I have this one class, GameControl, which I could instantiate and I want its methods exposed to other Javascript code on the page.
我想使用 GWT.我在 GWT 中有一个正在编译的正在运行的项目,但我不知道如何公开 GameControl 类的实例(+功能).
I thought to use GWT. I have a running project in GWT which compiles, but I can't figure out how to expose my instance (+functionality) of the GameControl class.
我认为使用 JSNI 来公开我的对象应该可以工作,但它没有.这是它现在的简短版本:
I thought using JSNI to expose my object should work, but it didn't. This is the short version of how it look like right now:
GameEntryPoint.java
import com.google.gwt.core.client.EntryPoint;
public class GameEntryPoint implements EntryPoint {
private GameControl _gameControl;
@Override
public void onModuleLoad() {
_gameControl = new GameControl();
expose();
}
public native void expose()/*-{
$wnd.game = this.@game.client.GameEntryPoint::_gameControl;
}-*/;
}
GameControl.java
package game.client;
public class GameControl {
public boolean isEmpty(int id){
// does stuff...
return true;
}
}
所以,GWT 确实编译了代码,我看到有一个 GameControl_0
对象正在构建并设置到 $wnd.game
中,但没有 isEmpty()
方法被找到.
So, GWT indeed compiles the code, and I see that there is a GameControl_0
object being built and set into $wnd.game
, but no isEmpty()
method to be found.
我预期的最终结果是将 window.game
作为 GameControl
的一个实例,并带有 GameControl
公开的所有公共方法.
My expected end result is to have a window.game
as an instance of GameControl
with all public methods GameControl
exposes.
我该怎么做?
编辑根据 @jusio
的回复,使用 JSNI 公开 window
属性是有效的,但它太冗长了.我正在尝试 gwt-exporter 解决方案.现在我有
Edit
As per @jusio
's reply, using JSNI to expose window
properties explicitly worked, but it was too verbose. I'm trying the gwt-exporter solution. Now I have
GameEntryPoint.java
package game.client;
import org.timepedia.exporter.client.ExporterUtil;
import com.google.gwt.core.client.EntryPoint;
public class GameEntryPoint implements EntryPoint {
@Override
public void onModuleLoad() {
ExporterUtil.exportAll();
}
}
RoadServer.java
package game.client;
import org.timepedia.exporter.client.Export;
import org.timepedia.exporter.client.ExportPackage;
import org.timepedia.exporter.client.Exportable;
@ExportPackage("game")
@Export("RoadServer")
public class RoadServer implements Exportable {
int _index;
int _id;
public RoadServer(int index,int id){
this._id=id;
this._index=index;
}
}
但仍然没有导出任何代码(特别是不是 RoadServer
).
but still none of the code is exported (specifically not RoadServer
).
推荐答案
您只公开了 GameControl
的实例.如果你想公开其他方法,你也必须公开它们.例如:
You have exposed only instance of the GameControl
. If you want to expose other methods, you'll have to expose them as well.
For example:
public native void expose()/*-{
var control = this.@game.client.GameEntryPoint::_gameControl;
var gameInstance = {
gameControl: control,
isEmpty:function(param){
control.@game.client.GameEntryPoint::isEmpty(*)(param);
}
}
$wnd.game = gameInstance;
}-*/;
还有一个名为 gwt-exporter 的框架,它可能会让事情变得更容易给你
Also there is a framework called gwt-exporter, it might make things easier for you
这篇关于如何在 GWT 中公开类功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!