gwt中java与js的相互调用
1. java通过jsni调用内部js
- Button button = new Button("java调用内部jsni的js方法");
- button.addClickHandler(new ClickHandler() {
- @Override
- public void onClick(ClickEvent event) {
- //gwt中java调用js方法
- execute("js方法被调用");
- }
- });
- /**
- * JSNI方法
- * @param id
- */
- public static native void execute(String str) /*-{
- alert(str);
- }-*/;
2. 内部js通过jsni调用java方法
- Button button1 = new Button("内部jsni的js调用java方法");
- button1.addClickHandler(new ClickHandler() {
- @Override
- public void onClick(ClickEvent event) {
- //gwt中java调用js方法
- executeJs("java方法被调用");
- }
- });
- /**
- * JSNI方法, 里面调用java方法 javaAlert
- * @param id
- */
- public static native void executeJs(String str) /*-{
- @com.hw.client.TestCall::javaAlert(Ljava/lang/String;)(str);
- }-*/;
3.gwt中java方法调用外部js
在gwt工程的index.html中加入外部方法
- <mce:script language="JavaScript"><!--
- function callOutJs(str){
- alert('此处是外部js方法:'+ str);
- }
- // --></mce:script>
然后在onModuleLoad中java方法进行调用
- Button button2 = new Button("JAVA调用外部js");
- button2.addClickHandler(new ClickHandler() {
- @Override
- public void onClick(ClickEvent event) {
- //gwt中java调用js方法
- callOutJS("外部js被调用");
- }
- });
- /**
- * JSNI方法 调用外部js方法
- * @param id
- */
- public static native void callOutJS(String str) /*-{
- $wnd.callOutJs(str);
- }-*/;
4. 外部js调用gwt的java方法
在onModuleLoad方法中调用 outJsCallGwt();
outJsCallGwt方法为
- /**
- * 需要被调用的js方法
- * @param id
- */
- private static native void outJsCallGwt() /*-{
- $wnd.outJsCallGwt = function (str) {
- alert("此处是gwt:"+ str);
- };
- }-*/;
在index.html中加入按钮以调用
- <button onclick="outJsCallGwt('外部按钮被点击')">点击</button>
现贴出application和index.html代码
- 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调用内部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被调用");
- }
- });
- RootPanel.get().add(button);
- RootPanel.get().add(button1);
- RootPanel.get().add(button2);
- outJsCallGwt();
- }
- /**
- * 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.hw.client.TestCall::javaAlert(Ljava/lang/String;)(str);
- }-*/;
- /**
- * 被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);
- };
- }-*/;
- }
- <!doctype html>
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <link type="text/css" rel="stylesheet" href="TestCall.css" mce_href="TestCall.css">
- <title>Web Application Starter Project</title>
- <mce:script language=JavaScript><!--
- function callOutJs(str){
- alert('此处是外部js方法:'+ str);
- }
- // --></mce:script>
- <mce:script type="text/javascript" language="javascript" src="testcall/testcall.nocache.js" mce_src="testcall/testcall.nocache.js"></mce:script>
- </head>
- <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('外部按钮被点击')">点击</button>
- <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>
备注: 以上html代码中<mce:script 应该为<script 由于csdn代码编辑器自动改变了值
- <script language=JavaScript>
- function callOutJs(str){
- alert('此处是外部js方法:'+ str);
- }
- <script>
gwt中java与js的相互调用的更多相关文章
- Unity3D中C#和js方法相互调用
通过查找资料,Unity3D中C#和js要相互调用彼此的方法,js文件必须放在"Standard Assets". "Pro Standard Assets" ...
- Hybrid App开发模式中, IOS/Android 和 JavaScript相互调用方式
IOS:Objective-C 和 JavaScript 的相互调用 iOS7以前,iOS SDK 并没有原生提供 js 调用 native 代码的 API.但是 UIWebView 的一个 dele ...
- .Net 与 Java 的服务接口相互调用
本文介绍.Net 与 Java 相互调用的例子.下面的介绍主要包括三方面:一是通过常用Web服务进行相互调用,二是使用TCP/IP套接字进行相互调用,三是使用Remote实现远程对象相互调用. 首先说 ...
- JAVA与.NET的相互调用——通过Web服务实现相互调用
JAVA与.NET是现今世界竞争激烈的两大开发媒体,两者语言有很多相似的地方.而在很多大型的开发项目里面,往往需要使用两种语言进行集成开发.而很多的开发人员都会偏向于其中一种语言,在使用集成开发的时候 ...
- JAVA与.NET的相互调用——利用JNBridge桥接模式实现远程通讯
分布式开发的历史 利用Remote方式调用远程对象实现服务器与客户端之间通讯是一种常用的网络开发方式,在.NET与JAVA开发当中,对Remote远程对象早已有着足够的支持(对Remote远程对象调用 ...
- 转载 OS js oc相互调用(JavaScriptCore) ---js调用iOS ---js里面直接调用方法
OS js oc相互调用(JavaScriptCore) 接着上节我们讲到的iOS调用js 下来我们使用js调用iOS js调用iOS分两种情况 一,js里面直接调用方法 二,js里面通过对象调用 ...
- 安卓中java和js如何交互
1.安卓中java和js如何交互 在Android上怎样实现JAVA和JS交互呢?Android的webview是基于webkit内核的,webview中集成了js与java互调的接口函数,通过add ...
- 转载 iOS js oc相互调用(JavaScriptCore) --iOS调用js
iOS js oc相互调用(JavaScriptCore) 从iOS7开始 苹果公布了JavaScriptCore.framework 它使得JS与OC的交互更加方便了. 下面我们就简单了解一下这 ...
- knockout中viewmodel跟子model相互调用
用knockout写前端复杂js逻辑的确很方便,而且html界面也很清爽. 在ko中对于复杂的业务逻辑我会给viewmodel创建一些子model对象,但是viewmodel跟子model怎样相互调用 ...
随机推荐
- 可视化查看MongoDB - MongoVUE
OS:Window 7 1.下载安装MongoVUE:http://www.mongovue.com/downloads/ 2.运行MongoVUE,点击“OK”按钮,使用免费版 3.选择一个已经存在 ...
- DSP:CCS V6 TMS320F2812 使用printf函数
使用Code Composer Studio Version: 6.1.1.00022,建立TMS320F2812工程. /* * main.c */ #include <stdio.h> ...
- MINA源码阅读之Future系
首先Future系是对某个异步操作完成的监听:即setValue()的完成情况监听:get/setValue其实是对result字段封装,由此,可以这样讲,Future系其实对于对result字段状态 ...
- html5应用程序标签
一.html5应用程序标签 (1)datalist需要数据载体 input list属性指向数据源 <input type="text" list="input_l ...
- Codeforces Round #316 div2
一场充满血腥hack之战!!! Problem_A: 题意: n个候选人在m个城市进行投票,每个城市选出票数最多的一个候选人为城市候选人,如果票数相同,则取编号小的候选人. 再从这m个城市候选人中选出 ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- backbone showcase
http://www.mhtml5.com/2012/06/5119.html http://tieba.baidu.com/p/2389371223 http://www.jdon.com/tags ...
- 修改PHP上传大小设置
目前文档文库上传大小是读取服务器的PHP环境的设置,你们的PHP环境上传限制是多少,这里显示的就是多少. 很多用户问我如何修改上传大小,自己可以百度一下方法,也可以根据以下步骤修改: 1.找到服务务器 ...
- ANDROID_MARS学习笔记_S03_007_GoogleMap1
一.简介 二.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <Linea ...
- pay lip service to
Understanding Progressive Enhancement中看到了这句话: Concerned with content availability, overall accessibi ...