2016-01-10 4 views
1

Я пытаюсь вызвать метод GWT с JavaScript, используя последнюю JsInterop - ниже головная часть моей HTMLGWT 2.8 beta1 - пытается вызвать метод Java с JavaScript, используя jsinterop

<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

<!--                --> 
<!-- Consider inlining CSS to reduce the number of requested files --> 
<!--                --> 
<link type="text/css" rel="stylesheet" href="Home.css"> 

<!--           --> 
<!-- Any title is fine       --> 
<!--           --> 
<title>Web Application Starter Project</title> 

<!--           --> 
<!-- This script loads your compiled module. --> 
<!-- If you add any GWT meta tags, they must --> 
<!-- be added before this line.    --> 
<!--           --> 
<script type="text/javascript" language="javascript" src="Home/Home.nocache.js"></script> 

</head> 

ниже является часть тела моего HTML

<body onload="myFunction()"> 

<script > 
function myFunction(){ 
    alert("something to say"); 
    var foo = new host.Employee(); 
    foo.hello("Gautam"); 
} 
</script> 

<!-- RECOMMENDED if your web app will not function without JavaScript enabled --> 

<h1>Web Application Starter Project</h1> 

<table align="center"> 
    <tr> 
    <td colspan="2" style="font-weight:bold;">Please enter your name:</td> 
    </tr> 
    <tr> 
    <td id="nameFieldContainer"></td> 
    <td id="sendButtonContainer"></td> 
    <td id="someButton"><input type="button" value="SomeValue" onclick=myFunction()/></td> 
    </tr> 
    <tr> 
    <td colspan="2" style="color:red;" id="errorLabelContainer"></td> 
    </tr> 
</table> 
</body> 

Вот класс Java

import com.google.gwt.user.client.Window; 

import jsinterop.annotations.JsMethod; 
import jsinterop.annotations.JsType; 

@JsType(namespace = "host" , name = "Employee") 
public class Employee implements Serializable { 

private String name ; 
private int age ; 
private long salary ; 

@JsMethod(name = "hello") 
public String sayHello(String name){ 
    String last = "Hello " + name ; 
    Window.alert(last); 
    return last ; 
} 
} 

В браузере вызывается тело onload = myFunction(), и первое предупреждение отображается всегда.

Но нажав на кнопку SomeValue показывает первое предупреждение от MYFUNCTION но ожидаемый host.Employee() всегда показывает неопределенными

Home.html:47 Uncaught ReferenceError: host is not defined 

Не могли бы вы указать мне на то, что здесь не так?

Я строю с gwt-maven-plugin для 2.8.0-beta1.

Update - вот мое содержание ПОМ 4.0.0

<parent> 
    <artifactId>bi-parent-client</artifactId> 
    <groupId>com.pa.bi</groupId> 
    <version>0.0.1-SNAPSHOT</version> 
    </parent> 
    <groupId>com.pa.bi</groupId> 
    <artifactId>bi-gwt-host</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>Maven Archetype for GWT</name> 
<properties> 
<!-- Convenience property to set the GWT version --> 
<gwtVersion>2.8.0-beta1</gwtVersion> 

<!-- GWT needs at least java 1.7 --> 
<maven.compiler.source>1.7</maven.compiler.source> 
<maven.compiler.target>1.7</maven.compiler.target> 

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 

<dependencyManagement> 
<dependencies> 
    <dependency> 
    <groupId>com.google.gwt</groupId> 
    <artifactId>gwt</artifactId> 
    <version>${gwtVersion}</version> 
    <type>pom</type> 
    <scope>import</scope> 
    </dependency> 
</dependencies> 
</dependencyManagement> 

<dependencies> 
<dependency> 
    <groupId>com.google.gwt</groupId> 
    <artifactId>gwt-servlet</artifactId> 
    <scope>runtime</scope> 
</dependency> 
<dependency> 
    <groupId>com.google.gwt</groupId> 
    <artifactId>gwt-user</artifactId> 
    <scope>provided</scope> 
</dependency> 
<dependency> 
    <groupId>com.google.gwt</groupId> 
    <artifactId>gwt-dev</artifactId> 
    <scope>provided</scope> 
</dependency> 
<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.11</version> 
    <scope>test</scope> 
</dependency> 
</dependencies> 

<build> 
<!-- Output classes directly into the webapp, so that IDEs and "mvn process- classes" update them in DevMode --> 
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> 

<plugins> 

    <!-- Mojo's Maven Plugin for GWT --> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>gwt-maven-plugin</artifactId> 
    <version>2.8.0-beta1</version> 
    <executions> 
     <execution> 
     <goals> 
      <goal>compile</goal> 
      <goal>test</goal> 
      <goal>generateAsync</goal> 
     </goals> 
     </execution> 
    </executions> 
    <!-- Plugin configuration. There are many available options, see 
     gwt-maven-plugin documentation at codehaus.org --> 
    <configuration> 
     <runTarget>Home.html</runTarget> 
     <modules> 
     <module>com.pa.bi.gwt.host.Home</module> 
     </modules> 
     <copyWebapp>true</copyWebapp> 
    </configuration> 
    </plugin> 
</plugins> 
</build> 
</project> 

Я добавил

<add-linker name="xsiframe" /> 
<set-configuration-property name="devModeRedirectEnabled" 
    value="true" /> 

в мой файл модуля Home.gwt.xml. Но все-таки не повезло

Update -

удалены jsInterOpMode флаг и добавил

<generateJsInteropExports>true</generateJsInteropExports> 

и мой Maven GWT: бег бросает странные ошибки -

[ERROR] Unable to find 'JS_RC.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source? 

Это обычно означает, что мне нужно чтобы наследовать модуль, и я искал такой модуль в проекте gwt на github, но пока не повезло.

ответ

3

Это известная проблема в GWT 2.8.0-beta1 при использовании -generateJsInteropExports с SDM; он исправлен, но еще не выпущен: https://github.com/gwtproject/gwt/issues/9223

Попробуйте использовать GWT 2.8.0-SNAPSHOT.

+0

Попробуй сейчас. Кроме того, ни один из доступных образцов не показывает простой пример этого. Я не заинтересован в использовании материалов vaadin и библиотек, которые обертывают все внутренние компоненты. – Gautam

+0

Где я могу получить снимки? Я не думаю, что они находятся на центральном уровне – Gautam

+0

Строго говоря, Central выпускает только хосты. Снимки GWT находятся на Sonatype OSSRH (иногда это называют «центральными моментальными снимками»), но в специальном репозитории: 'https: // oss.sonatype.org/content/repositories/google-snapshots /' –