How to call java GWT function from Javascript?

Is it possible to call Java methods (GWT) from Javascript? This is also unclear from the documentation. All samples here http://code.google.com/intl/en/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html demonstrate calling java functions from JSNI (not JS) functions.

UPDATE 1

Here is the Java code:

public class Test_GoogleWeb_JSNI_02 implements EntryPoint { /** * This is the entry point method. */ public void onModuleLoad() { } public static void Callee() { Window.alert("Callee"); } } 

Here are examples of call buttons in html:

 <input type='button' value='Call' onclick='Test02()'> 

And here are some functions that I tried and which did not work:

 <script type="text/javascript"> function Test01() { @com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee()(); } function Test02() { com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee()(); } </script> 

UPDATE 2

The following worked.

Java preparation:

 public void onModuleLoad() { Prepare(); } public static native void Prepare() /*-{ $doc.calleeRunner = @com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee(); }-*/; public static void Callee() { Window.alert("Callee"); } 

Subscriber:

 function Test03() { document.calleeRunner(); } 

Is there a better way?

+6
source share
1 answer

your example will not work as you are trying to use JSNI in some external script. If you want to call something from external JS, you need to use the approach described in this question , or use the GWT exporter

UPDATE:

The safest way to expose GWT material is to wrap the call in some other function. For instance:

  public native void expose()/*-{ $wnd.exposedMethod = function(param){ @com.my.MyClass::myFunction(*)(param); } }-*/; 

Otherwise, you may encounter some strange errors in production mode =)

+10
source

Source: https://habr.com/ru/post/904403/


All Articles