JavaScript Native Interface (JSNI) in GWT

Written by on March 9th, 2007 in Ajax News.

I have recently had quite a few people talking about how they like GWT but with they could get out of the Java box to delve into JavaScript for some low level work.

The assumption was that you cannot do this with GWT and that if you run against the abstraction barrier you are hosed.

There is actually a way to embed your own JavaScript directly.

JSNI has a similar name to JNI but isn’t as hairy, although it does have a creative hack.

To implement JSNI the GWT team overloaded the term native and used it for their own good:

JAVA:

  1.  
  2. public static native void alert(String msg) /*-{
  3.   $wnd.alert(msg);
  4. }-*/;
  5.  

Take a close look. Sneaky huh? The $wnd variable is your link to the window object, and if you need the DOM you simply use $doc.

The piece of JSNI that looks a little like JNI is how you access Java methods and fields directly from JavaScript.

Before you run off and start writing reams of JavaScript code, remember why you are using GWT in the first place, and use this as a last resort.

Source: Ajaxian
Original Article: http://ajaxian.com/archives/javascript-native-interface-jsni-in-gwt

Leave a Reply

You must be logged in to post a comment.



Site Navigation