如何从Javascript中调用GWT java函数? [英] How to call GWT java function from Javascript?

查看:139
本文介绍了如何从Javascript中调用GWT java函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Javascript调用Java(GWT)方法?从文档中也不清楚。所有示例均 http://code.google.com/intl/ru/webtoolkit/doc /latest/DevGuideCodingBasicsJSNI.html 演示从JSNI(非JS)函数调用Java函数。


$ b 更新1



这是一个Java代码:

  public class Test_GoogleWeb_JSNI_02 implements EntryPoint {
/ **
*这是入口点方法。
* /
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 Prepare()/ * - {
$ doc.calleeRunner = @ com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02 :: Callee();
} - * /;

public static void Callee(){
Window.alert(Callee);
}

来电者:

 函数Test03(){
document.calleeRunner();
}

有没有更好的方法?

您的示例不会起作用,因为您正试图在某些外部脚本中使用JSNI。如果你想从 external JS调用某些东西,你需要使用问题或使用 GWT出口商

a>


更新:



揭露GWT的最安全方法是在其他函数中包装调用。例如:

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

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

Is it possible to call Java (GWT) methods from Javascript? It is also unclear from documentation. All samples here http://code.google.com/intl/ru/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html demonstrate calling java functions from JSNI (not JS) functions.

UPDATE 1

Here is a Java code:

public class Test_GoogleWeb_JSNI_02 implements EntryPoint {
/**
 * This is the entry point method.
 */
public void onModuleLoad() {
}

public static void Callee() {
    Window.alert("Callee");
}
}

Here is caller button samples in html:

<input type='button' value='Call' onclick='Test02()'>

And here are some functions I tried and which were not worked:

<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>

UPDATE 2

The following worked.

Java preparation:

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");
}

Caller:

function Test03() {
        document.calleeRunner();
}

Is there a better way?

解决方案

your example is not going to work, since you are trying to use JSNI in some external script. If you want to call something from external JS you need to use approach described in this question or use GWT exporter

UPDATE:

The safest way to expose the GWT stuff is to wrap invocation in some other function. For example:

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

Otherwise you might encounter some strange bugs in production mode=)

这篇关于如何从Javascript中调用GWT java函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆