简体   繁体   English

从gwt返回值到javascript-jsni

[英]Returning value from gwt to javascript-jsni

I am trying to return a value from java function in GWT to javascript via JSNI 我正在尝试通过JSNI将GWT中的Java函数的值返回到javascript

    static public int call() { return 20; }

    public static native int jstest() /*-{
        try{
            val=@com.xxxx.package::call()();
            window.alert("Val:"+val);
            return $wnd.val;
        } catch(e) {
            console.error(e.message);
        }
    }-*/;

and in javascript alert(document.val); 并在javascript alert(document.val); , I end up with Exception Something other than an int was returned from JSNI method . ,我最终遇到异常JSNI方法返回了一个不同于int的东西 I guess I am messing up in returning value to javascript. 我想我搞砸了向javascript返回值。 Please let me know where I go wrong! 请让我知道我哪里出错了!

By declaring val as global it does get assigned on the window object (NOTE: not the $wnd objects). 通过将val声明为全局val,它确实在窗口对象上分配(注意:不是$ wnd对象)。 Sometimes with GWT those two are the same, sometimes they are not (it depends on the linker you are using). 有时对于GWT,这两个是相同的,有时却不相同(这取决于您使用的链接器)。

This is why you need to change your code to read 这就是为什么您需要更改代码以读取的原因

$wnd.val = @com.xxxx.package::call()();

or remove the global variable with: 或使用以下命令删除全局变量:

var val = @com.xxxx.package::call()();
return val;

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

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