1. Java calls internal JS via Jsni
Button button = New button ("Java calls the JS method of internal Jsni"); Button.addclickhandler (New ClickHandler () {@Override public void OnClick (Clickevent event) {//GWT Java calls the JS method execute (" JS method is called "); } }); /** * Jsni method * @param ID/public static native void execute (String str)/*-{alert (str); -*/;
2. Internal JS calls Java method via Jsni
Button button1 = New button ("Internal Jsni JS invoke Java method"); Button1.addclickhandler (New ClickHandler () {@Override public void OnClick (Clickevent event) {//GWT Java calling JS method Executejs ("Java method called"); } }); /** * Jsni method, which invokes Java method Javaalert * @param ID/public static native void Executejs (String str)/*-{@com. hw.client.Test Call::javaalert (ljava/lang/string;) (str); -*/;
3.GWT Java method calls external JS
Adding external methods to the index.html of GWT project
<mce:script language= "JavaScript" ><!--function Calloutjs (str) {alert (' Here is the external JS method: ' + str ';}//--></mce :script>
The Java method is then invoked in the Onmoduleload
Button button2 = New button ("Java calling external JS"); Button2.addclickhandler (New ClickHandler () {@Override public void OnClick (Clickevent event) {//GWT Java calling JS method Calloutjs ("External JS is called"); } }); /** * Jsni method calls external JS method * @param ID/public static native void Calloutjs (String str)/*-{$wnd. Calloutjs (str); -*/;
4. External JS invoke GWT's Java method
Call OUTJSCALLGWT () in the Onmoduleload method;
The OUTJSCALLGWT method is
/** * need to be called JS method * @param ID/private static native void Outjscallgwt ()/*-{$wnd. OUTJSCALLGWT = function (str) {alert ( "Here is GWT:" + str);} -*/;
Add a button to the index.html to invoke the
<button onclick= "OUTJSCALLGWT (' external button clicked ')" > click </button>
The application and index.html codes are now posted
Package com.hw.client; Import Com.google.gwt.core.client.EntryPoint; Import com.google.gwt.event.dom.client.ClickEvent; Import Com.google.gwt.event.dom.client.ClickHandler; Import Com.google.gwt.user.client.Window; Import Com.google.gwt.user.client.ui.Button; Import Com.google.gwt.user.client.ui.RootPanel; public class Testcall implements entrypoint {public void Onmoduleload () {button button = New button ("Java calls the JS method of internal Jsni") ; Button.addclickhandler (New ClickHandler () {@Override public void OnClick (Clickevent event) {//GWT Java calls the JS method execute (" JS method is called "); } }); Button button1 = New button ("Internal Jsni JS invoke Java method"); Button1.addclickhandler (New ClickHandler () {@Override public void OnClick (Clickevent event) {//GWT Java calling JS method Executejs ("Java method called"); } }); Button button2 = New button ("Java calling external JS"); Button2.addclickhandler (New ClickHandler () {@Override public void OnClick (Clickevent event) {//GWT Java calling JS method Calloutjs ("External JS is called"); } }); Rootpanel.get (). Add (button); Rootpanel.get (). Add (button1); Rootpanel.get (). Add (Button2); OUTJSCALLGWT (); /** * Jsni method calls external JS method * @param ID/public static native void Calloutjs (String str)/*-{$wnd. Calloutjs (str); -*/; /** * Jsni method * @param ID/public static native void execute (String str)/*-{alert (str); -*/; /** * Jsni method, which invokes Java method Javaalert * @param ID/public static native void Executejs (String str)/*-{@com. hw.client.Test Call::javaalert (ljava/lang/string;) (str); -*/; /** * is called by the JS method * @param ID/public static void Javaalert (String str) {window.alert (str);}/** * The JS method to be invoked * @param ID * /private static native void Outjscallgwt ()/*-{$wnd. OUTJSCALLGWT = function (str) {alert ("Here is GWT:" + str);} -*/; }
<!doctype html>
Note: <mce:script in the above HTML code should be <script because the CSDN code Editor automatically changes the value
<script language=javascript> function Calloutjs (str) {alert (' Here is the external JS method: ' + str '); } <script>