gpt4 book ai didi

java - 如何从 Javascript 调用 GWT java 函数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:15:28 29 4
gpt4 key购买 nike

是否可以从 Javascript 调用 Java (GWT) 方法?文档中也不清楚。所有 sample 都在这里 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();
}

有没有更好的办法?

最佳答案

您的示例无法运行,因为您正尝试在某些外部脚本中使用 JSNI。如果您想从外部 JS 调用某些内容,您需要使用此question 中描述的方法。或使用 GWT exporter

更新:

公开 GWT 内容的最安全方法是将调用包装在其他一些函数中。例如:

    public native void expose()/*-{
$wnd.exposedMethod = function(param){
@com.my.MyClass::myFunction(*)(param);
}
}-*/;

否则你可能会在生产模式中遇到一些奇怪的错误=)

关于java - 如何从 Javascript 调用 GWT java 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8619245/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com