1

I am trying to invoke a gwt method from javascript using latest JsInterop - below is the head part of my html

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

below is the body part of my 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>

Here is the java class

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

In the browser the body onload=myFunction() gets called and the first alert is shown always.

But clicking the button someValue shows the first alert from myFunction but the expected host.Employee() always shows a undefined

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

Could you point me to what's wrong here ?

I am building with gwt-maven-plugin for 2.8.0-beta1.

Update - here is my pom content 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>

I added the

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

to my Home.gwt.xml module file. But still no luck

Update -

removed jsInterOpMode flag and added

<generateJsInteropExports>true</generateJsInteropExports>

and my maven gwt:run throws a weird error -

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

It would typically mean I need to inherit a module and I looked for a such a module in gwt project on github but so far no luck.

1 Answer 1

3

This is a known issue in GWT 2.8.0-beta1 when using -generateJsInteropExports with SDM; it's been fixed but not yet released: https://github.com/gwtproject/gwt/issues/9223

Try using GWT 2.8.0-SNAPSHOT.

Sign up to request clarification or add additional context in comments.

5 Comments

Will try it out now. Also none of the samples available actually show a plain example of doing this. I am not keen on using vaadin stuff and libraries that wrap all the internals.
where can I get the snapshots ? I don't think they are on maven central
Strictly speaking, Central only hosts releases. GWT snapshots are on Sonatype OSSRH (sometimes thought of as "central snapshots"), but on a special repository: https://oss.sonatype.org/content/repositories/google-snapshots/
I added the said repo to my local jfrog artifactory and I can see the jars there through browse. But due to some reason my eclipse and .m2 folder do not receive it. a bit strange for me
I forgot to add the new repo configured in artifactory to the virtual repo exposed i.e. remote repos.. and now jsinterop works like a charm :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.