Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GWT:可以从外部JavaScript而不是JSNI调用Java方法吗?_Javascript_Gwt_Jsni - Fatal编程技术网

GWT:可以从外部JavaScript而不是JSNI调用Java方法吗?

GWT:可以从外部JavaScript而不是JSNI调用Java方法吗?,javascript,gwt,jsni,Javascript,Gwt,Jsni,我正试图将我的大部分原生JavaScript代码从JSNI方法中移出并放入脚本中,并利用原生JSNI方法调用这些外部方法 现在我的一个点击处理程序遇到了问题。当用户单击特定元素时,JSNI方法会执行一些基于JQuery的动画,然后在回调中调用Java方法。一个简单的例子是: public native void attachClickHandler(SomeCustomPanel customPanel) /*-{ $wnd.jQuery("#theElement").click(fun

我正试图将我的大部分原生JavaScript代码从JSNI方法中移出并放入脚本中,并利用原生JSNI方法调用这些外部方法

现在我的一个点击处理程序遇到了问题。当用户单击特定元素时,JSNI方法会执行一些基于JQuery的动画,然后在回调中调用Java方法。一个简单的例子是:

public native void attachClickHandler(SomeCustomPanel customPanel) /*-{
    $wnd.jQuery("#theElement").click(function() {
        // some JQuery animation logic here...
        $wnd.jQuery("#theElement").animate({ top: "500px" }, 500, function() {
            customPanel.@com.something.whatever.client.SomeCustomPanel::doSomething()();
        });
        // some other code here...
    });
}-*/;
这个代码有效。它按照预期进行编译和工作。我想把它转换成一个外部JavaScript。我尝试了以下方法。我将其放在外部JavaScript中:

function attachClickAction(customPanel) {
    $("#theElement").click(function() {
        // other stuff...
        $("#theElement").animate({ top: "500px" }, 500, function() {
            customPanel.@com.something.whatever.client.SomeCustomPanel::doSomething()();
        });
        // other stuff...
    });
}
MYOBJ.doSomething();
并对本机函数进行如下修改:

public native void attachClickHandler(SomeCustomPanel customPanel) /*-{
    $wnd.attachClickAction(customPanel);
}-*/;
但这是不正确的。JavaScript文件甚至不会加载,因为这不是正确的JavaScript。(Chome的开发工具给我错误消息“uncaughtSyntaxError:意外标识符”。)

有没有办法从外部JavaScript文件而不是从JSNI方法调用Java方法?


如果有必要的话,我会使用GWT2.4。

答案是否,您不能从外部JavaScript函数显式调用Java方法。

但是,您可以很聪明地利用JavaScript允许您将函数本身作为参数传递的事实

我修改了JavaScript函数,如下所示:

function attachClickAction(callbackFunction) {
    $("#theElement").click(function() {
        // other stuff...
        $("#theElement").animate({ top: "500px" }, 500, callbackFunction);
    // other stuff...
    });
}
在我的Java代码中,我显式地将Java方法作为回调函数参数传递:

public native void attachClickHandler(SomeCustomPanel customPanel) /*-{
    $wnd.attachClickAction(function() {
        customPanel.@com.something.whatever.client.SomeCustomPanel::doSomething()();
    });
}-*/;

这样,当JSNI方法编译时,它会正确地调用
doSomething()
Java方法,并且该方法调用会正确地全部传递给外部JavaScript函数。

尽管您不能使用vanilla GWT直接从JavaScript调用Java类的方法, 执行从本机JavaScript调用“Java”对象的方法所需的大量管道

你可以这样做:

爪哇:

JavaScript:

function attachClickAction(customPanel) {
    $("#theElement").click(function() {
        // other stuff...
        $("#theElement").animate({ top: "500px" }, 500, function() {
            customPanel.@com.something.whatever.client.SomeCustomPanel::doSomething()();
        });
        // other stuff...
    });
}
MYOBJ.doSomething();

gwt exporter与GWT2.4配合得很好,

是的,您可以将Java(gwt)方法导出为JavaScript函数,并且可以将Java对象放入JavaScript变量中。下面是一个简单的例子:

@覆盖
moduleload()上的公共void{
exportJavaMethodsToJs();
最终SomeCustomPanel firstCustomPanel=新的SomeCustomPanel(“第一”);
exportJavaObjectToJs(“firstCustomPanel”,firstCustomPanel);
最终SomeCustomPanel anotherCustomPanel=新的SomeCustomPanel(“另一个”);
exportJavaObjectToJs(“另一个CustomPanel”,另一个CustomPanel);
}
本机void exportJavaMethodsToJs()/*-{
$wnd.doSomething=功能(面板){
panel@com.something.whatever.client.SomeCustomPanel::doSomething();
}
//在此处导出JavaScript中所需的任何方法。。。
}-*/;
本机void exportJavaObjectToJs(最终字符串键,最终对象)/*-{
$wnd[key]=对象;
}-*/;
然后,在JavaScript中(在调用onModuleLoad()之后!),您可以像

doSomething(第一个自定义面板);
剂量测定法(另一组);
您的示例现在如下所示:

public native void attachClickHandler(SomeCustomPanel customPanel) /*-{
    $wnd.attachClickAction(customPanel);
}-*/;
jQuery(“#元素”)。单击(函数(){
//这里有一些JQuery动画逻辑。。。
$wnd.jQuery(“#theElement”).animate({top:“500px”},500,function(){
剂量测定法(第一组);
});
//这里还有其他一些代码。。。
});

不幸的是,我无法添加外部依赖项,但这是您的建议。注意:您应该使用
$entry()
@avashopt()围绕调用
customPanel…doSomething()
,为什么?我以前从未见过这种构造。您能解释一下吗?它将调用包装在一些try/catch语句周围,这样GWT/javascript中的Java代码异常就不会到达纯javascript代码空间。