【问题标题】:Failed to export Java class to JavaScript via JsInterop in GWT无法通过 GWT 中的 JsInterop 将 Java 类导出到 JavaScript
【发布时间】:2018-03-10 21:12:15
【问题描述】:

我正在关注 GWT 文档 Coding Basics - JavaScript: JsInterop,通过注解 @JsMethod 将 Java 类导出到 JavaScript。但是,Java 类不会转译成 JavaScript。

这是我的 Java 类:

package io.mincongh.client;

import jsinterop.annotations.JsMethod;

public class ExportedMethods {

  @JsMethod
  public static String sayHello(String name) {
    return "Hello, " + name;
  }
}

我的项目是在 Maven 中构建的,通过 GWT Maven 插件 2.8.2:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>gwt-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>test</goal>
        <goal>generateAsync</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <runTarget>StockMarket.html</runTarget>
    <modules>
      <module>io.mincongh.StockMarket</module>
    </modules>
  </configuration>
</plugin>

当我在浏览器的控制台中调用导出的方法时。那么方法没有定义:

io.mincongh.client.ExportedMethods.sayHello('world');

VM59:1 Uncaught ReferenceError: io is not defined 在:1:1

【问题讨论】:

    标签: javascript java gwt gwt-jsinterop


    【解决方案1】:

    来自规范JsInterop v1.0: Nextgen GWT/JavaScript Interoperability,段落@JsType:

    请注意,将 Java 对象导出到 JavaScript 以供其命名空间(例如此示例)访问需要 --generateJsInteropExports 标志。

    所以你需要在 Maven GWT 插件中指定这个标志:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>gwt-maven-plugin</artifactId>
      ...
      <configuration>
        <generateJsInteropExports>true</generateJsInteropExports>
      </configuration>
    </plugin>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多