Calling a GWT Java function from an html script tag
Asked Answered
S

1

14

I have a GWT project and I would like to add a script tag to the main html file of the GWT project that calls a Java function located in my client code.

According to the documentation I should add something like the following html tag:

<script type='text/javascript'> 
[email protected]::myFunction();
</script>

where com.myCompany.myProject.client.myClass is the class path and myFunction is the java function I would like to call.

When I try this with the following implementation of myFunction nothing happens:

public void myFunction() {
    HTMLPanel panel = new HTMLPanel("I have been called");
    RootPanel.get().add(panel);
}

That is, myFunction is not being called.

But when I make the same call from a JSNI method, then it works.

Is it maybe not possible to do the call from an html script, or am I doing something wrong?

Thanks!

Saker answered 27/3, 2011 at 19:39 Comment(0)
E
13
  1. What you are trying to do does not work because GWT compiler renames all identifier names to minimize produced code size: so myFunction() exists, but it's called something else.

  2. You were looking at old version of documentation. In the latest version this is all explained: Calling a Java Method from Handwritten JavaScript

The solution - add an additional method somewhere:

public static native void exportMyFunction() /*-{
   $wnd.myFunction =
      $entry(@com.myCompany.myProject.client.myClass::myFunction());
}-*/;

then in your app initialization you must call EnclosingClass.exportMyFunction(). Then in hand-crafted javascript you can access it via:

window.myFunction();
Eddington answered 27/3, 2011 at 20:28 Comment(7)
I am trying to solve a similar problem, just a question, how to pass String to myFunction()? like myFunction(String s) ?Choric
Look atbthis doc to see hoe to add arguments to JSNI calls: code.google.com/webtoolkit/doc/latest/…Eddington
Also in this answer there are complete examples: #5235235Eddington
How does this work if myFunction() is an instance method and not static? How does the JavaScript know which instance of myFunction() to call? It doesn't. I'd be curious to hear if this solved the problem, because I have the same situation and it throws an exception for that reason. I don't think you can call a non-static GWT Java method from handwritten JavaScript, because in your app initialization when you export the native function, you don't have an instance of EnclosingClass yet, and you have a chicken-and-egg problem.Lemar
@axle123 it's all in the docs: gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#methodsEddington
I re-read the docs and still come to the same conclusion. In order to put the [instance-expr.] before the @className, you'd have to pass the instance as an argument to exportMyFunction. But you don't have the instance yet on app initialization.Lemar
This works for calling an Java instance method from a native method, but not from handwritten JavaScript. That's because you have to do an intial export of your method to JavaScript (just like this answer and the docs show). But how would you export a single JavaScript function that can be used in the future to call instance methods for instances that don't even exist yet? There's a reason why the example in the docs for "Calling a Handwritten Java Method From JavaScript" shows two static methods. If anyone can do that with an instance method I'll buy you a six-pack.Lemar

© 2022 - 2024 — McMap. All rights reserved.