无法通过 GWT 中的 JsInterop 将 Java class 导出到 JavaScript

Failed to export Java class to JavaScript via JsInterop in GWT

我正在按照 GWT 文档 Coding Basics - JavaScript: JsInterop 将 Java class 通过注释 @JsMethod 导出到 Java 脚本。然而,Java class 没有被转译成 JavaScript.

这是我的 Java class:

package io.mincongh.client;

import jsinterop.annotations.JsMethod;

public class ExportedMethods {

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

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

<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 at :1:1

来自规范 JsInterop v1.0: Nextgen GWT/JavaScript 互操作性,段落@JsType:

Note that exporting of Java Objects to JavaScript to be accessed by their namespace (e.g. this sample) requires --generateJsInteropExports flag.

因此您需要在 Maven GWT 插件中指定此标志:

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