10

是否可以从 Javascript 调用 Java (GWT) 方法?从文档中也不清楚。这里的所有示例http://code.google.com/intl/ru/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html演示了从 JSNI(不是 JS)函数调用 java 函数。

更新 1

这是一个Java代码:

public class Test_GoogleWeb_JSNI_02 implements EntryPoint {
/**
 * This is the entry point method.
 */
public void onModuleLoad() {
}

public static void Callee() {
    Window.alert("Callee");
}
}

这是 html 中的呼叫者按钮示例:

<input type='button' value='Call' onclick='Test02()'>

以下是我尝试过的一些功能,但没有奏效:

<script type="text/javascript">

    function Test01() {
        @com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee()();
    }

    function Test02() {
        com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee()();
    }


</script>

更新 2

以下工作。

Java准备:

public void onModuleLoad() {
    Prepare();
}

public static native void Prepare() /*-{
    $doc.calleeRunner = @com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee();
}-*/;

public static void Callee() {
    Window.alert("Callee");
}

呼叫者:

function Test03() {
        document.calleeRunner();
}

有没有更好的办法?

4

1 回答 1

13

您的示例不起作用,因为您试图在某些外部脚本中使用 JSNI。如果您想从外部JS 调用某些东西,您需要使用此问题中描述的方法或使用GWT 导出器

更新:

公开 GWT 内容的最安全方法是将调用包装在其他函数中。例如:

    public native void expose()/*-{
    $wnd.exposedMethod = function(param){
         @com.my.MyClass::myFunction(*)(param);
    }
}-*/;

否则你可能会在生产模式下遇到一些奇怪的错误=)

于 2011-12-23T18:28:20.963 回答