简体   繁体   中英

Returning value from gwt to javascript-jsni

I am trying to return a value from java function in GWT to javascript via JSNI

    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); , I end up with Exception Something other than an int was returned from JSNI method . I guess I am messing up in returning value to 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). Sometimes with GWT those two are the same, sometimes they are not (it depends on the linker you are using).

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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