gwt中 java、js互相调用

代码部分
package com.tet.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;

/**
* Entry point classes define onModuleLoad().
*/
public class Zftet implements EntryPoint {

  @Override
  public void onModuleLoad() {
    Button button = new Button("java调用内部jsni的js方法");
    button.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        // gwt中java调用js方法
        execute("js方法被调用");

      }
    });

    Button button1 = new Button("内部jsni的js调用java方法");
    button1.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        // gwt中java调用js方法
        executeJs("java方法被调用");

      }
    });

    Button button2 = new Button("JAVA调用外部js");
    button2.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        // gwt中java调用js方法
        callOutJS("外部js被调用");

      }
    });
    logout();
    outJsCallGwt();
    outJsCallGwt1();
    RootPanel.get().add(button);
    RootPanel.get().add(button1);
    RootPanel.get().add(button2);

  }

  private static native void SetWPPic(String xx) /*-{
top.SetWPPic(xx);
  }-*/;

  /**
   * JSNI方法 调用外部js方法
   *
   * @param id
   */
  public static native void callOutJS(String str) /*-{
$wnd.callOutJs(str);
  }-*/;

  /**
   * JSNI方法
   *
   * @param id
   */
  public static native void execute(String str) /*-{
alert(str);
  }-*/;

  /**
   * JSNI方法, 里面调用java方法 javaAlert
   *
   * @param id
   */
  public static native void executeJs(String str) /*-{
@com.tet.client.Zftet::javaAlert(Ljava/lang/String;)(str);
  }-*/;

  native void logout() /*-{
$wnd.logout = function() {
@com.tet.client.Zftet::test()();
};
  }-*/;

  static void test() {
    Window.alert("++++");
  }

  static void test(String value) {
    Window.alert(value);
  }

  /**
   * 被js方法调用
   *
   * @param id
   */
  public static void javaAlert(String str) {
    Window.alert(str);
  }

  /**
   * 需要被调用的js方法
   *
   * @param id
   */
  private static native void outJsCallGwt() /*-{
$wnd.outJsCallGwt = function(str) {
alert("此处是gwt:" + str);
};
  }-*/;

  /**
   * 需要被调用的js方法
   *
   * @param id
   */
  private static native void outJsCallGwt1() /*-{
$wnd.outJsCallGwt1 = function(str) {
@com.tet.client.Zftet::test(Ljava/lang/String;)(str)();
};
  }-*/;
}


HTML 页面

<!doctype html>

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="Zftet.css">
<title>Web Application Starter Project</title>


<script type='text/javascript'>
function WPSelectPic() {
window.external.notify("wp");
}

function SetWPPic(url) {
document.getElementById('File1').value = url;
window.alert(url);
}

function getActivityID() {
var id = "a6940e1e-fbbb-4fae-a233-9451e0854fc8";
return id;
}
</script>

<script type='text/javascript'>
function callOutJs(str) {
alert('此处是外部js方法:' + str);
}
</script>

<script type="text/javascript" language="javascript"
src="zftet/zftet.nocache.js"></script>
</head>

<!--                                           -->
<!-- The body can have arbitrary html, or      -->
<!-- you can leave the body empty if you want  -->
<!-- to create a completely dynamic UI.        -->
<!--                                           -->
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" mce_src="javascript:''"
id="__gwt_historyFrame" tabIndex='-1'
style="position: absolute; width: 0; height: 0; border: 0"></iframe>

<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
<noscript>
<div
style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled in order for this
application to display correctly.</div>
</noscript>
<h1>Web Application Starter Project</h1>
<table align="center">
<tr>
<td colspan="2" style="font-weight: bold;"
mce_style="font-weight:bold;">Please enter your name:</td>
</tr>
<tr>
<button onclick="outJsCallGwt('外部按钮被点击')">点击1</button>
<button onclick="outJsCallGwt1('外部按钮被点击')">点击2</button>
<a href='javascript:;' onclick='logout()'>注销</a>
<td id="nameFieldContainer"></td>
<td id="sendButtonContainer"></td>
</tr>
<tr>
<td colspan="2" style="color: red;" mce_style="color:red;"
id="errorLabelContainer"></td>
</tr>
</table>
</body>
</html>

你可能感兴趣的:(gwt)