Mutual invocation of Java and JS in GWT __js

Source: Internet
Author: User

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>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.