从javascript调用非静态get java方法的可能性

时间:2012-05-04 11:47:34

标签: java javascript gwt jsni exporter

一般目标:从javascript调用一些非静态java方法

描述: 在Java Applet代码中查找DOM上的gwt小部件并将其称为java方法(非静态)


JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject gwtWidget = (JSObject) doc.call("getElementsByName", widgetName);

//todo: have possibility to call `exported` java method over here, smth like:
//Object[] params = new Object[1];
//params[0] = widgetName;
//Object result = gwtWidget.call("exportedJavaMethod", params);

//todo: or just call global ($wnd) static JSNI bridge method:
//Object result = win.call("exportedJavaMethod", params);
//

问题是:我可以通过widget的id找到而不是widget,但是它的DivElement没有任何导出的实例化方法。

我的widget类是Exportable(gwt-export):


@Export(value="somewidget")
public class SomeWidget extends SimplePanel implements ..., Exportable {
    private final String id = "id_some_widget_" + count++;
    private static int count = 0;

    public SomeWidget () {
        getElement().setAttribute("name", id);
        getElement().setId(id);
    }
    ...

    public static Element getInstanceById(String elementId) {
        Element someWidget= DOM.getElementById(elementId);
        return someWidget;
    }

    public String getSomeInstancedData() {
        return "useful_inner_data" + this.id;
    }

所以,例如,我想找到添加到DOM的具体小部件,并在javascript中调用非静态方法getSomeInstancedData()。它有可能吗?

假设smth像:


var someWidget = document.getElementById(widgetId);
alert(someWidget.getSomeInstancedData());

//or:
var someWidgetExported = com.mypackage.widgets.somewidget.getInstanceById(listenerId);
alert(someWidgetExported.getSomeInstancedData());

在Base模块中,我写道:


ExporterUtil.exportAll();

有一个View(ViewWithSomeWidget.ui.xml)包含这个小部件:

...
base:Form
base:SomeWidget ui:field="someWidget" ...
...
/base:SomeWidget
...

当SomeWidget没有实现Exportable时,项目运行良好,但我无法调用找到的widget的DIV元素的非静态方法。

到时候,为了解决问题,SomeWidget实现了Exportable,但是由于使用延迟绑定的ClassCastException,progect没有显示具有SomeWidget的View:


ClassCastException: com.mypackage.widgets.SomeWidgetExporterImpl cannot be cast to com.mypackage.widgets.SomeWidget

那么,可能还有其他方法可以找到widget的javascript对象并调用它的导出java方法吗?无论如何,任何想法都非常感激。

3 个答案:

答案 0 :(得分:11)

您可以声明一个javascript函数,它将触发非静态方法。

例如:

package com.example;
public class Layout extends VLayout() {
    private String nonStaticVar = "nonStaticVar";
    public Layout() {
        declareMethod(this);
    }

    //Method which declares non-static method in javascript
    public native void declareMethod(Layout layout) /*-{
        var layout = layout;
        $wnd.doSomething = function(someString) {
            layout.@com.example.Layout::doSomething(Ljava/lang/String;)(someString);
        }
    }-*/;

    //Non static method which is being called
    public String doSomething(String someString) {
        //Do something, e.g. set var in this instantiated class, or output stuff
        this.nonStaticVar = someString;
        Window.alert(someString);
    }
}

现在,打电话     doSomething的( “BLA”); 从javascript将调用您的非静态方法。

答案 1 :(得分:4)

你可以在div元素上定义一个自己的jsmethod,它可以调用一个widget-method

@Export(value="somewidget")
public class SomeWidget extends SimplePanel implements ..., Exportable {
    private final String id = "id_some_widget_" + count++;
    private static int count = 0;

    public SomeWidget () {
        getElement().setAttribute("name", id);
        getElement().setId(id);
        attachInstanceHook(getElement());
    }
    ...

    public String getSomeInstancedData() {
        return "useful_inner_data" + this.id;
    }


    private native void attachInstanceHook(Element element) /*-{
        var elem = element;
        var widget = this;
        elem.getWidgetData = function () {
            widget.@your.package.name.SomeWidget::getSomeInstancedData()();
        };

    }-*/;

你的js-code应该是这样的:

var someWidget = document.getElementById(widgetId);  //the id of the div element
alert(someWidget.getWidgetData());

答案 2 :(得分:1)

而不是尝试通过其DOM元素获取窗口小部件,首先将其Java实例注入全局范围并引用它:

public class SomeWidget extends SimplePanel implements ..., Exportable {

    //...

    public SomeWidget () {
        getElement().setAttribute("name", id);
        getElement().setId(id);

        attachToGlobalScope(id, this);
    }

    private native void attachToGlobalScope(String id, SomeWidget instance) /*-{
        $wnd.shared[id] = instance;
    }-*/;

    //...
}

比以后更新Applet API层中的相应提取:

JSObject win = JSObject.getWindow(this);
JSObject shared = (JSObject) win.getMember("shared");
Widget widgetRef = (Widget) shared.getMember(widgetId);

免责声明:这个有点弯曲的解决方案继承了JavaScript全局范围变量,这被认为是一般的不良做法,但这里作为唯一的共同范围,因此被滥用于对象存储。