How to expose class functionality in GWT

给你一囗甜甜゛ 提交于 2019-12-01 05:38:41

问题


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).


回答1:


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




回答2:


This may help.

http://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en



来源:https://stackoverflow.com/questions/10029656/how-to-expose-class-functionality-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!