我有一个用Java编写的类库,并希望将其转换为Javascript。所有方法都非常简单,主要与操作集合有关。我有一个类GameControl,我可以实例化它,我希望它的方法暴露给页面上的其他Javascript代码。
我想用GWT。我在GWT中有一个正在运行的项目,但是我无法弄清楚如何公开GameControl类的实例(+功能)。
我认为使用JSNI来暴露我的对象应该可行,但事实并非如此。这是它现在的样子的简短版本:
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()
方法。
我预期的最终结果是window.game
作为GameControl
的实例,并公开所有公开方法GameControl
。
我该怎么做?
修改
根据{{1}}的回复,使用JSNI公开显式@jusio
属性,但它太冗长了。我正在尝试gwt-exporter解决方案。现在我有了
GameEntryPoint.java
window
RoadServer.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();
}
}
但仍然没有导出任何代码(特别是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;
}
}
)。
答案 0 :(得分:6)
您只展示了GameControl
的实例。如果要公开其他方法,则还必须公开它们。
例如:
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的框架,它可能会让您更轻松
答案 1 :(得分:1)