I am a newcomer to GWT and JavaScript. There are similar question of this type I have tried to follow, but I keep failing.
I have a GWT app, I need to call a Java function from Javascript( on the onclick of a href tag, in particular.) Following is what I have done.
public class JSNITest {
public static void handleAnchorClick(int a , int b) {
Window.alert("Current row and Column is " + a + " " + b);
}
public static native void exportMyFunction()/*-{
$wnd.handleAnchorClick = function(param1,param2){
@company.package.class.JSNITest::handleAnchorClick(*)(param1,param2);
}-*/;
}
And in the HTML,
<a href="javascript:handleAnchorClick(a1,a2);">link</a>
(a1 , a2)
are two integer variables in my code.
I have also called EnclosingClass.exportMyFunction() in the entry point function.
I keep running into various kinds of exceptions(No Such class exception). Can someone please correct me?
Regards
Let me explain a bit more about exporting GWT stuff to the JS world. You have several options to do that, but I will focus on three methods.
[EDITED]
0- JsInterop: GWT maintainers are working in a new feature to easily export java methods to javascript, and wrap javascript objects. The feature was very experimental in 2.7.0 lacking some features, but in 2.8.0 will be almost functional. Please take a look to the Design Document, and other discussions in the mailing list.
[END]
1- JSNI: The first one is to write your own jsni, in this case you have to be aware about the possible mistakes you could make. Basically these mistakes are because you have to know how to deal with types. In your case if you want to get a javascript array (like you are asking in your comment below), the solution could be:
Note that JSNI only allows you to pass
primitive
types (except long) andJavaScriptObject
objects. So when passing a javascript array, you have to receive it with aJavaScriptObject
like in the example. In this case, since javascript only uses a type for numbers,args.getNumber
will return always a double, and you have to convert in java.2- gwt-exporter For exporting large projects, or when you need to handle complex objects and classes I'd rather use gwt-exporter
gwt-exporter will deal with any kind of primitive types (even with long)
myfunc(long[] args)
, with var-argsmyfunc(long...args)
, it supports method overload, and much more.3- gwtquery Finally if you prefer gwtquery, you can use a technique to add function properties to any js object like
window
With gquery you can use the gwt
JsArrayMixed
class which always returns a number as a double, or you can use aJsCache
which allows to convert numbers to any other numeric type in java((JsCache)ary.get(1, Integer.class)
As a summary, I would rather use gwt-exporter as the first option because it is specialized in handling this problematic. As a second option, I would use gquery which is a serious complement to gwt. Finally, I would avoid to use hand-written jsni when possible, Javascript is normally a source of issues and mistakes (think that the main goal of gwt is not to deal with js).
You should consider GWT exporter. You might even consider waiting because GWT 2.8 should come out pretty soon. It was supposed to come out some time around The begging of 2015. 2015 has already begun and they're demonstrating at GWT.create right now so it should come out any day now. If you can't wait then you can either use experimental is interop, JSNI like the top answer says to or GWT exporter. JSNI is more complicated and involves a lot of boiler plate code so if you have to do a lot of js interop I recommend GWT-exporter.