如何在GWT中公开类的功能 [英] How to expose class functionality in GWT

查看:109
本文介绍了如何在GWT中公开类的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Java编写的类库,想将它转换为Javascript。所有的方法都非常简单,主要与操作集合有关。我有一个类GameControl,我可以实例化,我希望它的方法暴露给页面上的其他Javascript代码。



我以为使用GWT。我在GWT中编译了一个正在运行的项目,但我无法弄清楚如何公开我的GameControl类的实例(+功能)。

我认为使用JSNI公开我的对象应该可行,但事实并非如此。这是现在它的简短版本:



GameEntryPoint.java

  import com.google.gwt.core.client.EntryPoint; 

public class GameEntryPoint实现EntryPoint {

private GameControl _gameControl;

@Override
public void onModuleLoad(){
_gameControl = new GameControl();
expose();


$ b public native void expose()/ * - {
$ wnd.game = this。@ game.client.GameEntryPoint :: _ gameControl;
} - * /;




GameControl.java p>

  package game.client; 
public class GameControl {
public boolean isEmpty(int id){
// does stuff ...
return true;






$ b因此,GWT确实编译了代码,我看到有一个 GameControl_0 对象被构建并设置为 $ wnd.game ,但没有 isEmpty()方法被找到。



我预期的最终结果是有一个 window.game 作为 GameControl的一个实例与所有公共方法 GameControl 公开。



我该怎么做?

编辑
按照 @jusio 的回复,使用JSNI公开窗口属性显式工作,但它太冗长。我正在尝试gwt-exporter解决方案。现在我有了 $ b

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;
导入org.timepedia.exporter.client.Exportable;

$ b @ExportPackage(game)
@Export(RoadServer)
public class RoadServer实现可导出{
int _index;
int _id;
public RoadServer(int index,int id){
this._id = id;
this._index = index;


$ / code $ / pre

但仍然没有导出代码 RoadServer )。

解决方案

C $ C> 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 ,它可能让你更容易


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.

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.

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;
    }   
}

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.

My expected end result is to have a window.game as an instance of GameControl with all public methods GameControl exposes.

How can I do this?

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;
    }
}

but still none of the code is exported (specifically not RoadServer).

解决方案

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;
    }-*/;

Also there is a framework called gwt-exporter, it might make things easier for you

这篇关于如何在GWT中公开类的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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