gpt4 book ai didi

gwt 2.8.0 jsinterop 导出不工作

转载 作者:行者123 更新时间:2023-12-05 02:19:32 24 4
gpt4 key购买 nike

查看此文档: https://docs.google.com/document/d/10fmlEYIHcyead_4R1S5wKGs1t2I7Fnp_PaNaa7XTEk0/edit

导出一个java类并在javascript中保留它的方法名,应该和注释一个类一样简单,但似乎行不通。

步骤如下:

  • 在 Eclipse 中创建一个新的 GWT Web 应用程序。 Project name="jsinterop", package="com.example.jsinterop",使用 GWT2.8.0 并取消选中 App Engine,然后单击完成。
  • 创建新类

    package com.example.jsinterop.client;

    import jsinterop.annotations.*;

    @JsType
    public class Foo {
    public int x;
    public int y;

    public int sum() {
    return x + y;
    }
    }
  • 将 JSNI 方法添加到入口点类

    public static native int callfoo() /*-{
    var foo = new com.example.jsinterop.client.Foo();
    foo.x = 1;
    foo.y = 2;

    return foo.sum();

    }-*/;
  • 在入口点调用此方法。

    callfoo();

  • 运行该应用程序会在 Chrome 中产生如下 javascript 错误:

    Uncaught ReferenceError: com is not defined
    at rb_g$ (Jsinterop.java:155)
    at qb_g$.sb_g$ [as onModuleLoad_0_g$] (Jsinterop.java:77)
    at Array.cyc_g$ (com_00046example_00046jsinterop_00046Jsinterop__EntryMethodHolder.java:3)
    at initializeModules_0_g$ (ModuleUtils.java:44)
    at MJ_g$ (Impl.java:239)
    at PJ_g$ (Impl.java:298)
    at Impl.java:77
    at vxc_g$ (ModuleUtils.java:55)
    at StringHashCache.java:23

    如何让这个简单的实现发挥作用?有什么我想念的吗?

    提前致谢。

    最佳答案

    默认情况下不导出任何内容,您必须通过将 --generateJsInteropExports 传递给 GWT(编译器或 super 开发模式)来请求它。

    请注意,下一版本的 GWT 2.x 将允许您将要导出的包/类列入白名单,而不是当前的全有或全无。

    关于gwt 2.8.0 jsinterop 导出不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482103/

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