【发布时间】:2012-01-27 00:02:37
【问题描述】:
是否可以从 Javascript 调用 Java (GWT) 方法?从文档中也不清楚。 http://code.google.com/intl/ru/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html 这里的所有示例都演示了从 JSNI(不是 JS)函数调用 java 函数。
更新 1
这是一段Java代码:
public class Test_GoogleWeb_JSNI_02 implements EntryPoint {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
}
public static void Callee() {
Window.alert("Callee");
}
}
这是 html 中的调用者按钮示例:
<input type='button' value='Call' onclick='Test02()'>
下面是一些我尝试过但没有成功的功能:
<script type="text/javascript">
function Test01() {
@com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee()();
}
function Test02() {
com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee()();
}
</script>
更新 2
以下工作。
Java 准备:
public void onModuleLoad() {
Prepare();
}
public static native void Prepare() /*-{
$doc.calleeRunner = @com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee();
}-*/;
public static void Callee() {
Window.alert("Callee");
}
来电者:
function Test03() {
document.calleeRunner();
}
有没有更好的办法?
【问题讨论】:
-
你能调用一个返回对象的javascrypt方法吗?并使用那个对象?
标签: java javascript gwt jsni