简体   繁体   English

是否可以将Java方法的引用传递给GWT中的javascript

[英]is it possible to pass reference for java method to javascript in GWT

I'm trying to implement multi-threading in client side by using VKthread with GWT, 我正在尝试通过在GWT中使用VKthread在客户端实现多线程,

my problem that i need to pass a reference for java method to thread invoker in java script, so that when the thread is executed, it call the java method 我的问题是我需要将java方法的引用传递给java脚本中的线程调用程序,以便在执行线程时调用java方法

here's my code: 这是我的代码:

static JavaScriptObject sum() {

    return flipName();
}

static native JavaScriptObject flipName() /*-{
    console.log("asd");
}-*/;


native String Excec() /*-{

    $wnd.vkthread
            .exec(
                    @com.tradeos.b2b.industries.freight.data.freightTrade.marketplace.builders.client.MarketPlaceQuoteResultsPanel::sum()(), //function to execute in a threads
                    [], //arguments for the function
                    function(data) { // callback function;
                        var foo = data;
                        console.log(foo);
                    });

}-*/;

If i passed a javascript method to $wnd.vkthread it works well, but when trying to pass java method it doesn't work! 如果我向$ wnd.vkthread传递了javascript方法,则效果很好,但是在尝试传递java方法时,则无法正常工作!

In your snippet, you are calling your method ( ::sum()() ), to get a reference to the method, remove the last pair of parentheses. 在代码段中,您正在调用方法( ::sum()() ),以获取对该方法的引用,并删除最后一对括号。 You should also wrap it into $entry : 您还应该将其包装到$entry

$wnd.vkthread
        .exec(
                $entry(@com.tradeos.b2b.industries.freight.data.freightTrade.marketplace.builders.client.MarketPlaceQuoteResultsPanel::sum()), //function to execute in a threads
                [], //arguments for the function
                function(data) { // callback function;
                    var foo = data;
                    console.log(foo);
                });

You need to export your method: 您需要导出方法:

public static native void exportStaticMethod() /*-{
       $wnd.sum =
           $entry(@com.tradeos.b2b.industries.freight.data.freightTrade.marketplace.builders.client.MarketPlaceQuoteResultsPanel::sum());
}-*/;

Now you can use $wnd.sum instead of your Java method which is convenient if you plan to call it in more than one place. 现在,您可以使用$ wnd.sum而不是Java方法,如果打算在多个位置调用它,这将很方便。

If you only use it once, you can simply wrap it into $entry() directly in your JSNI method. 如果只使用一次,则可以直接在JSNI方法中将其包装到$ entry()中。

The documentation explains it: Calling a Java Method from Handwritten JavaScript 该文档对此进行解释: 从手写JavaScript调用Java方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM