This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

GWT in Action: GWT and my actual Javascript code (JSNI)

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m interested in the JavaScript Native Interface to use directly Javascript code in Java. Looking at the index of the book (http://www.manning.com/hanson/excerpt_index.pdf) I found out that there are probably 10-15 pages covering this topic.

From your experience is it practical to integrate Javascript code within the Java one? Do you think is well covered this topic in the book?

Can I ask you if in your experience using a the java-to-javascript compiler you really stop thinking about the underlying Javascript?


Thank you guys
Filippo
 
author
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Filippo.

From your experience is it practical to integrate Javascript code within the Java one?



Yes. Check out the GWT Widget Library. It contains wrapper classes for Scriptaculous, JsGraphics, JsCalendar, etc. These are just JSNI wrappers around JS code.

These are really great JS libraries. Using JSNI allows you to take advantage of that and not have to reinvent the wheel.

Do you think is well covered this topic in the book?



Yes. JSNI sounds complicated, but it is pretty simple. In the book we cover the special JSNI syntax, and show you how to use it.

Can I ask you if in your experience using a the java-to-javascript compiler you really stop thinking about the underlying Javascript?



There is a compiler?

It took me about two weeks to get used to it, and now I don't even think about it. I just write my code in Java, compile, and deploy. I don't look at the generated code at all.
 
author
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Filippo,

Yes, it's certainly practical to integrate JavaScript within your GWT code.

Take a look at the Google Search and Google Video Search applications that comes with the book's main example (http://dashboard.manning-sandbox.com and then select the Create Menu). These example use JSNI to communicate from GWT to Google JavaScript libraries.

Whilst it is possible, you do have to think of the reasons why you would.

The main reasons for using JSNI are two-fold:

1) You need to use a JavaScript library that you cannot rewrite in GWT (e.g. you don't have the license; you don't have the desire to,...).

2) You need to do something that is not currently provided by GWT.

I don't think it is sensible to advocate putting large amounts of JavaScript within an application except for those two reasons. To do so, you would:

* not benefit from the static analysis of GWT compiler (so your code would be larger than necessary - GWT compiler very aggressively removes unused Java code),
* not leverage off the browser agnostic approaches within GWT (so code would either be larger with included code for browser sniffing/browser versions, or not work in one/more browsers),
* not be able to take advantage of the typing model of Java (so potential for errors / tougher debugging creeps in)

The principles of JSNI are fairly straight forward - it effectively hijacks JNI syntax for methods and then introduces some syntax to represent Java objects in your JavaScript code.

The chapter in the book talks through the key components of JSNI covering writing JavaScript in your code; talking to JavaScript libraries; getting JavaScript libraries to talk back to GWT applications; passing objects over the boundaries; and implementing an API to your application through JSNI. This is then backed up by complete implementations in the example application.

Your final question is also interesting! There is a danger when writing a GWT application to forget the actual client side Java code will be implementated in JavaScript. If you look on the GWT forum there are often questions about accessing a user's hard drive; directly accessing a database; client side logging (e.g. Log4J file output) - stuff that is not too unusual to think of in a Java program, but not necessarily possible in JavaScript. That said, as long as you remember your code will eventually become JavaScript, it is a comforting development experience.

Hope the answer to your questions are in there - naybe I should start writing shorter answers!!

//Adam
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!