Problem with Java class in GWT JsInterop

Having some trouble with JsInterop while wrapping up a piece of JavaScript code.

The JavaScript code looks like this:

com = { gwidgets: {} };
com.gwidgets.Spring = function () {
        this.name = "hello";    
};

com.gwidgets.Spring.prototype.getName = function () {return "test";
};

Here is the JsInterop class:

package com.gwidgets.leaflet;

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

@JsType(isNative=true)
public class Spring {

    @JsMethod
    public native String getName();
}

However, I encounter an error when I try to instantiate the class and call the getName() method:

leafletwrapper-0.js:1183 Uncaught TypeError: spring.getName is not a function

Can anyone help me identify what's wrong with my code?

Answer №1

One way to solve the issue in javascript is by assigning a namespace to the annotation.

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

@JsType(isNative=true, namespace = "com.gwidgets")
public class Spring {

    @JsMethod
    public native String getName();
}

Another option is to relocate the java class Spring to the package com.gwidgets, aligning it with the javascript.

Alternatively, you can modify the namespace in the javascript to correspond with the package in the Spring class.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show the HTML page returned by the server on the client side

Hey there! I've come across a PHP code snippet that sends back an HTML file as a response. This PHP code makes use of the include method to send the file. When I'm on the client side, I'm employing AJAX. Upon typing console.log(data) (with ...

eliminating labels from a string through recursive method

One of my challenges involves developing a function that can remove tags from an input string. For example: '<strong>hello <em>my name <strong>is</strong> </em></strong>' The desired result should be: &apos ...

Efficient method of delivering cohesive information from database to user without the need for continuous querying

Within the database, there is stored data about each user that typically remains constant. However, occasionally a user may update their information, such as changing their name. This data includes the user's name, username, and company details. The ...

Navigating a Bootstrap modal using arrow keys for scrolling - up and down

I attempted to address this issue by using the following code: // here, 'this' represents the modal $(this).focus(); Despite trying the above solution and even attempting to trigger a click event on it, I was unable to get it to work! UPDATE ...

Are we utilizing this JavaScript function properly for recycling it?

Two functions have been implemented successfully. One function adds the autoplay attribute to a video DOM element if the user is at a specific section on the page. The other function smoothly slides in elements with a transition effect. The only limitatio ...

ChromeDriver does not respond to setHeadless() method from Selenium

I'm attempting to utilize Chrome in headless mode for Selenium tests by following the instructions on the official Google Developers page. Here's my code snippet: public static void main(String[] args) throws Exception { RemoteWebDriver driv ...

Save the content of the textbox on the webpage automatically, no need for a webservice or settimeout

Is there a way to save the text entered in a textbox on a webpage automatically, without relying on a webservice or the settimeout function? Any help would be greatly appreciated.Thank you in advance. ...

The PHP function is failing to communicate with jQuery and Ajax

Having trouble with PHP return to jQuery/Ajax functionality, When I try to edit an item, the error message displays even though the success function is executed. On the other hand, when attempting to delete an item, nothing is displayed despite the succes ...

JSON Array Position Sequence

I have a system that takes input in the form of lines of text stored as an array, for example: array[123,556,"test",0,0]. By using val().split('\n'), I am able to add each new line to the array so that each line index is incremented by 1. He ...

The NDK located in the ~/Library/Android/sdk/ndk-bundle directory for Flutter was missing a source.properties file

After launching the Flutter app in vscode, I encountered the following error message in CMD: FAILURE: Build failed with an exception. Issue: Execution failed for task ':app:stripDebugDebugSymbols'. NDK at C:\Users\jupun\AppData&b ...

Get the first object from the array that matches any value in the input array by utilizing Java 8

My selection of values includes 1,2,3,4. The way I structure these values can vary, such as using an Array, ArrayList, etc. In addition to my list of values, I receive a response from a rest call that may or may not include these values. My goal is to ret ...

Slick.js integrated with 3D flip is automatically flipping after the initial rotation

I'm encountering an issue with my CSS3 carousel and 3D flipping. Whenever I navigate through the carousel and flip to the next slide, the first slide seems to automatically flip/flop after completing the rotation. You can see a visual demonstration o ...

NuxtLink sending users to incorrect destination URL

I am facing an issue with my static generated Nuxt site. Everything works perfectly fine when I host it locally, but once I load it on GitHub Pages, the NuxtLink elements' hrefs are incorrect. For instance, one of my links looks like this: <NuxtLi ...

The Vue router-view is mistakenly loading the parent component instead of displaying its own content

Here is a simple route configuration: { path: '/', component: Home, }, This route configuration sets the path to the home page and loads the Home component when the path is '/'. However, I am encountering an issue where the Po ...

I'm finding it difficult to grasp the purpose of $inject within controllers

I'm feeling completely lost when it comes to understanding inject in Angular. I can't seem to grasp where it should be utilized and its purpose. Is it specifically tied to factory methods, as outlined here? myController.$inject = ['$scope&a ...

Currently in the process of uploading a 30MB XML file for autocomplete

Currently, I am dealing with a large 30 MB XML file containing numerous words. I am considering two options for utilizing autocomplete effectively: loading the entire XML into an array or creating a cloud-based database and accessing the words through Res ...

Encountering a Meteor issue during the installation of npm packages

When attempting to install cheerio through npm, I encountered a specific error message. Error: Can't set DOCTYPE here. (Meteor sets <!DOCTYPE html> for you) - line 1, file Similarly, when trying to use jsdom, I faced a parse error. Currently ...

Dynamically select checkbox groups based on items already listed in a textarea

Hello! Would you be able to review this example and provide guidance on how I can use jQuery to dynamically select the checkboxes that have the same text as in the textarea below? <br /> <br /> Default Items From Database <br /> < ...

Only consider valid values for input and ignore any zeros

I am working on a form where I need to accept any number, regardless of if it's negative, a float, or a long integer. I have implemented code to not allow null, undefined, or empty values, but I encountered an issue where entering 0 is being read as e ...

What is the best approach for handling an AJAX request on the server side?

Consider the function below: $.ajax({url:"http://127.0.0.1:8080", data: "123", success: function(response, textStatus, jqXHR) { alert(response); }, error: function(jqXHR, textStatus, errorThrown) { alert("An er ...