简体   繁体   中英

Call JSNI Java function from javascript via a frame in GWT - $wnd is undefined error

I know I'm probably just doing something stupid but I cannot find an example that actually shows how to call a Java method from javascript using GWT.

I've followed the documentation almost verbatim where is says:

package mypackage;

public class Account {
private int balance = 0;
public int add(int amt) {
  balance += amt;
}

public native void exportAdd() /*-{
    var that = this;
    $wnd.add = $entry(function(amt) {
      that.@mypackage.Account::add(I)(amt);
    });
}-*/;
}

Then you can call it in JS using

$wnd.add(5);

But that results in an error for me that says "$wnd is undefined".

This is my code: I export the function call

public native void exportPaymentProcessComplete()/*-{
    var that = this;
    console.log('exportingPaymentProcessComplete');
    $wnd.paymentProcessComplete = $entry(function(result){
        that.@com.ec.client.checkout.Presenter::paymentProcessComplete(Ljava/lang/String;)(result);
    });

}-*/;

I have a simple function that's being called (with a breakpoint because I've yet to get it to be called)

public void paymentProcessComplete(String result){
    if(result != null){

    }
}

This is the tricky part and probably where I'm going wrong. The JSNI call is being made from an iframe when it loads. I think it has to do with trying to call the parent window's javascript functions but I'm not sure how to refer to the parents $wnd object.

I've tried this:

response.getWriter().print("<script type=\"text/javascript\">parent.$wnd.paymentProcessComplete(\"SUCCESS\");</script>");

Which is when I get the "$wnd is undefined" error.

And also this:

response.getWriter().print("<script type=\"text/javascript\">parent.paymentProcessComplete(\"SUCCESS\");</script>");

Which gives me a "Unable to get property 'paymentProcessComplete' of undefined or null reference". Which is basically the same error as "$wnd is undefined".

Anyone have any thoughts on how to accomplish this?

when compiling your GWT App $wnd is replaced by window . So when you are trying to call a exported method from inside an iframe call it like this:
window.parent.paymentProcessComplete("SUCCESS")

After some deeper digging, I discovered that the JSNI code that was exporting/exposing my JAVA method was throwing a Cast exception because it was trying to attach it to the Presenter class it was a part of instead of the Window.

So this code:

public native void exportPaymentProcessComplete()/*-{
    var that = this;
    console.log('exportingPaymentProcessComplete');
    $wnd.paymentProcessComplete = $entry(function(result){
        that.@com.ec.client.checkout.Presenter::paymentProcessComplete(Ljava/lang/String;)(result);
    });
}-*/;

Became this code:

public native void exportPaymentProcessComplete()/*-{
    $wnd.paymentProcessComplete = $entry(function(result){
        @com.ra.ec.client.checkout.CheckoutPresenter::paymentProcessComplete(Ljava/lang/String;)(result);
    });     
}-*/;

Which also meant the paymentProcessComplete() method had to have a static modifier applied to it's declaration.

private static void paymentProcessComplete(String result){

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