0

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

2 Answers 2

1

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")

Sign up to request clarification or add additional context in comments.

1 Comment

That did appear to change things. I'm getting a slightly different error "Object doesn't support property or method 'paymentProcessComplete'". It sounds like I'm referencing "$wnd" correctly now, at least. Thank you. :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.