1. java通过jsni调用内部js

  1. Button button = new Button("java调用内部jsni的js方法");
  2. button.addClickHandler(new ClickHandler() {
  3. @Override
  4. public void onClick(ClickEvent event) {
  5. //gwt中java调用js方法
  6. execute("js方法被调用");
  7. }
  8. });
  9. /**
  10. * JSNI方法
  11. * @param id
  12. */
  13. public static native void execute(String str) /*-{
  14. alert(str);
  15. }-*/;

2. 内部js通过jsni调用java方法

  1. Button button1 = new Button("内部jsni的js调用java方法");
  2. button1.addClickHandler(new ClickHandler() {
  3. @Override
  4. public void onClick(ClickEvent event) {
  5. //gwt中java调用js方法
  6. executeJs("java方法被调用");
  7. }
  8. });
  9. /**
  10. * JSNI方法,  里面调用java方法 javaAlert
  11. * @param id
  12. */
  13. public static native void executeJs(String str) /*-{
  14. @com.hw.client.TestCall::javaAlert(Ljava/lang/String;)(str);
  15. }-*/;

3.gwt中java方法调用外部js

在gwt工程的index.html中加入外部方法

  1. <mce:script language="JavaScript"><!--
  2. function callOutJs(str){
  3. alert('此处是外部js方法:'+ str);
  4. }
  5. // --></mce:script>

然后在onModuleLoad中java方法进行调用

  1. Button button2 = new Button("JAVA调用外部js");
  2. button2.addClickHandler(new ClickHandler() {
  3. @Override
  4. public void onClick(ClickEvent event) {
  5. //gwt中java调用js方法
  6. callOutJS("外部js被调用");
  7. }
  8. });
  9. /**
  10. * JSNI方法 调用外部js方法
  11. * @param id
  12. */
  13. public static native void callOutJS(String str) /*-{
  14. $wnd.callOutJs(str);
  15. }-*/;

4.  外部js调用gwt的java方法

在onModuleLoad方法中调用  outJsCallGwt();

outJsCallGwt方法为

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

在index.html中加入按钮以调用

  1. <button onclick="outJsCallGwt('外部按钮被点击')">点击</button>

现贴出application和index.html代码

  1. package com.hw.client;
  2. import com.google.gwt.core.client.EntryPoint;
  3. import com.google.gwt.event.dom.client.ClickEvent;
  4. import com.google.gwt.event.dom.client.ClickHandler;
  5. import com.google.gwt.user.client.Window;
  6. import com.google.gwt.user.client.ui.Button;
  7. import com.google.gwt.user.client.ui.RootPanel;
  8. public class TestCall implements EntryPoint {
  9. public void onModuleLoad() {
  10. Button button = new Button("java调用内部jsni的js方法");
  11. button.addClickHandler(new ClickHandler() {
  12. @Override
  13. public void onClick(ClickEvent event) {
  14. //gwt中java调用js方法
  15. execute("js方法被调用");
  16. }
  17. });
  18. Button button1 = new Button("内部jsni的js调用java方法");
  19. button1.addClickHandler(new ClickHandler() {
  20. @Override
  21. public void onClick(ClickEvent event) {
  22. //gwt中java调用js方法
  23. executeJs("java方法被调用");
  24. }
  25. });
  26. Button button2 = new Button("JAVA调用外部js");
  27. button2.addClickHandler(new ClickHandler() {
  28. @Override
  29. public void onClick(ClickEvent event) {
  30. //gwt中java调用js方法
  31. callOutJS("外部js被调用");
  32. }
  33. });
  34. RootPanel.get().add(button);
  35. RootPanel.get().add(button1);
  36. RootPanel.get().add(button2);
  37. outJsCallGwt();
  38. }
  39. /**
  40. * JSNI方法 调用外部js方法
  41. * @param id
  42. */
  43. public static native void callOutJS(String str) /*-{
  44. $wnd.callOutJs(str);
  45. }-*/;
  46. /**
  47. * JSNI方法
  48. * @param id
  49. */
  50. public static native void execute(String str) /*-{
  51. alert(str);
  52. }-*/;
  53. /**
  54. * JSNI方法,  里面调用java方法 javaAlert
  55. * @param id
  56. */
  57. public static native void executeJs(String str) /*-{
  58. @com.hw.client.TestCall::javaAlert(Ljava/lang/String;)(str);
  59. }-*/;
  60. /**
  61. * 被js方法调用
  62. * @param id
  63. */
  64. public static void javaAlert(String str){
  65. Window.alert(str);
  66. }
  67. /**
  68. * 需要被调用的js方法
  69. * @param id
  70. */
  71. private static native void outJsCallGwt() /*-{
  72. $wnd.outJsCallGwt = function (str) {
  73. alert("此处是gwt:"+ str);
  74. };
  75. }-*/;
  76. }
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  5. <link type="text/css" rel="stylesheet" href="TestCall.css" mce_href="TestCall.css">
  6. <title>Web Application Starter Project</title>
  7. <mce:script language=JavaScript><!--
  8. function callOutJs(str){
  9. alert('此处是外部js方法:'+ str);
  10. }
  11. // --></mce:script>
  12. <mce:script type="text/javascript" language="javascript" src="testcall/testcall.nocache.js" mce_src="testcall/testcall.nocache.js"></mce:script>
  13. </head>
  14. <body>
  15. <!-- OPTIONAL: include this if you want history support -->
  16. <iframe src="javascript:''" mce_src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
  17. <!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
  18. <noscript>
  19. <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">
  20. Your web browser must have JavaScript enabled
  21. in order for this application to display correctly.
  22. </div>
  23. </noscript>
  24. <h1>Web Application Starter Project</h1>
  25. <table align="center">
  26. <tr>
  27. <td colspan="2" style="font-weight:bold;" mce_style="font-weight:bold;">Please enter your name:</td>
  28. </tr>
  29. <tr>
  30. <button onclick="outJsCallGwt('外部按钮被点击')">点击</button>
  31. <td id="nameFieldContainer"></td>
  32. <td id="sendButtonContainer"></td>
  33. </tr>
  34. <tr>
  35. <td colspan="2" style="color:red;" mce_style="color:red;" id="errorLabelContainer"></td>
  36. </tr>
  37. </table>
  38. </body>
  39. </html>

备注: 以上html代码中<mce:script  应该为<script 由于csdn代码编辑器自动改变了值

  1. <script language=JavaScript>
  2. function callOutJs(str){
  3. alert('此处是外部js方法:'+ str);
  4. }
  5. <script>

gwt中java与js的相互调用的更多相关文章

  1. Unity3D中C#和js方法相互调用

    通过查找资料,Unity3D中C#和js要相互调用彼此的方法,js文件必须放在"Standard Assets". "Pro Standard Assets" ...

  2. Hybrid App开发模式中, IOS/Android 和 JavaScript相互调用方式

    IOS:Objective-C 和 JavaScript 的相互调用 iOS7以前,iOS SDK 并没有原生提供 js 调用 native 代码的 API.但是 UIWebView 的一个 dele ...

  3. .Net 与 Java 的服务接口相互调用

    本文介绍.Net 与 Java 相互调用的例子.下面的介绍主要包括三方面:一是通过常用Web服务进行相互调用,二是使用TCP/IP套接字进行相互调用,三是使用Remote实现远程对象相互调用. 首先说 ...

  4. JAVA与.NET的相互调用——通过Web服务实现相互调用

    JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方.而在很多大型的开发项目里面,往往需要使用两种语言进行集成开发.而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候 ...

  5. JAVA与.NET的相互调用——利用JNBridge桥接模式实现远程通讯

    分布式开发的历史 利用Remote方式调用远程对象实现服务器与客户端之间通讯是一种常用的网络开发方式,在.NET与JAVA开发当中,对Remote远程对象早已有着足够的支持(对Remote远程对象调用 ...

  6. 转载 OS js oc相互调用(JavaScriptCore) ---js调用iOS ---js里面直接调用方法

    OS js oc相互调用(JavaScriptCore)   接着上节我们讲到的iOS调用js 下来我们使用js调用iOS js调用iOS分两种情况 一,js里面直接调用方法 二,js里面通过对象调用 ...

  7. 安卓中java和js如何交互

    1.安卓中java和js如何交互 在Android上怎样实现JAVA和JS交互呢?Android的webview是基于webkit内核的,webview中集成了js与java互调的接口函数,通过add ...

  8. 转载 iOS js oc相互调用(JavaScriptCore) --iOS调用js

    iOS js oc相互调用(JavaScriptCore)   从iOS7开始 苹果公布了JavaScriptCore.framework 它使得JS与OC的交互更加方便了. 下面我们就简单了解一下这 ...

  9. knockout中viewmodel跟子model相互调用

    用knockout写前端复杂js逻辑的确很方便,而且html界面也很清爽. 在ko中对于复杂的业务逻辑我会给viewmodel创建一些子model对象,但是viewmodel跟子model怎样相互调用 ...

随机推荐

  1. 可视化查看MongoDB - MongoVUE

    OS:Window 7 1.下载安装MongoVUE:http://www.mongovue.com/downloads/ 2.运行MongoVUE,点击“OK”按钮,使用免费版 3.选择一个已经存在 ...

  2. DSP:CCS V6 TMS320F2812 使用printf函数

    使用Code Composer Studio  Version: 6.1.1.00022,建立TMS320F2812工程. /* * main.c */ #include <stdio.h> ...

  3. MINA源码阅读之Future系

    首先Future系是对某个异步操作完成的监听:即setValue()的完成情况监听:get/setValue其实是对result字段封装,由此,可以这样讲,Future系其实对于对result字段状态 ...

  4. html5应用程序标签

    一.html5应用程序标签 (1)datalist需要数据载体 input list属性指向数据源 <input type="text" list="input_l ...

  5. Codeforces Round #316 div2

    一场充满血腥hack之战!!! Problem_A: 题意: n个候选人在m个城市进行投票,每个城市选出票数最多的一个候选人为城市候选人,如果票数相同,则取编号小的候选人. 再从这m个城市候选人中选出 ...

  6. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  7. backbone showcase

    http://www.mhtml5.com/2012/06/5119.html http://tieba.baidu.com/p/2389371223 http://www.jdon.com/tags ...

  8. 修改PHP上传大小设置

    目前文档文库上传大小是读取服务器的PHP环境的设置,你们的PHP环境上传限制是多少,这里显示的就是多少. 很多用户问我如何修改上传大小,自己可以百度一下方法,也可以根据以下步骤修改: 1.找到服务务器 ...

  9. ANDROID_MARS学习笔记_S03_007_GoogleMap1

    一.简介 二.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <Linea ...

  10. pay lip service to

    Understanding Progressive Enhancement中看到了这句话: Concerned with content availability, overall accessibi ...