[Java] GWT中Java与JS互相调用

博客首页 » Java GWT中Java与JS互相调用

发布于 09 Dec 2014 05:37
标签 blog
gwt中 java、js互相调用

http://zf0512305.iteye.com/blog/2147545

gwt
代码部分
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>


本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Java GWT中Java与JS互相调用

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License