Search code examples
javagwtjsni

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{
            [email protected]::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!


Solution

  • 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;