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怎样相互调用 ...
随机推荐
- vs2012生成的项目,如何在只装有VS2010的电脑上打开
步骤: 1.用记事本打开Vs2012生成的项目解决方案文件(.sln文件)文件 2.修改前两行 Microsoft Visual Studio Solution File, Format Versio ...
- STM32与S3C2440的区别
一.定位 STM32: 高功能单片机.工业控制 S3C2440: 处理器.智能设备 二.跑系统 STM32: ucos-II S3C2440: Linux等大型系统 三.硬件架构 STM32: C ...
- python学习之--自定义函数:
Python之--自定义函数: 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回. 以下自定义 ...
- ubuntu修改grub2
转自修改系统启动项 grub2配置的方法 ubuntu 在早期的Ubuntu中,使用Grub作为系统的启动引导程序,想修改系统启动项非常简单,只要用gedit打开系统菜单设定文件( sudo gedi ...
- linux 监控
http://www.iyunv.com/thread-50606-1-1.html http://segmentfault.com/a/1190000002537665 http://blog.cs ...
- 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 【SGU 390】Tickets (数位DP)
Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell ...
- 常用mysql命令(经常更新)
insert into hr_t_clubschedule(clubid) select clubid from hr_t_clubschedule where id=45;//获取指定数据,并插入数 ...
- Android Animation
Android中常用两种动画模式,tween animation和frame animation,即补间动画和帧动画,但在android3.0中又引入了一个新的动画系统:property animat ...
- PAIP.提升效率----论项目知识库的建设。。
PAIP.提升效率----论项目知识库的建设.. 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net ...