如何在GWT中公开类功能
How to expose class functionality in GWT
我有一个用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
公开了所有公共方法。
我该怎么做?
编辑根据@jusio
的回复,使用JSNI公开window
属性显式有效,但过于冗长。我正在尝试gwt出口商解决方案。现在我有
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
)。
您只公开了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的框架,它可能会让变得更容易
这可能会有所帮助。
http://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en
- 添加文字和评论功能更新Div
- JavaScript打印功能使日历停止工作
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 如何防止网页加载后自动启动功能
- 除修剪外的其他功能
- 悬停功能触发器
- 如何在GoogleWeb工具包(GWT)中从JSNI调用接口(实例化)
- 使用angularjs向浏览器发送servlet响应(下载功能)
- 删除CKEditor工具栏按钮,但不删除功能
- 异步facebook功能
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- jQuery滚动功能只工作一次
- Graphiti中是否有任何工具提示功能
- React redux初始化功能,无论状态变化如何
- 在哪里可以学习ECMAScript标准中尚未包含的JavaScript功能
- 正在获取select上的功能id
- 如何在GWT(或Java AWT Swing)中使用相同的事件处理程序对相同类型的多个对象执行相同的功能
- 通过SWT浏览器/ SWT浏览器功能从GWT应用程序调用Java方法
- 如何在GWT中公开类功能
- 通过JSNI在GWT中实现类似java.lang.reflect.Proxy的功能