Tuesday, March 18, 2014

Call GWT Java Code from JavaScript

It is very easy to call javascript from GWT java code by making use of JSNI. But calling GWT java code from external java script of JSNI is little tricky and it becomes more complicate when you have to call instance method of a GWT java class. For more detail refer GWT official document (refer)

Call static class method from JavaScript

GWTCode.java


package open.pandurang.client.view;

/**
 * @author Pandurang Patil 18-Mar-2014
 * 
 */
public class GWTCode {

 public static String hello(String name) {
  return "Hello " + name + "!";
 }

 public static native void exportMethod() /*-{
  $wnd.gwtcode_hello = function(name) {
   return @open.pandurang.client.view.GWTCode::hello(Ljava/lang/String;)(name);
  };
 }-*/;
}

At line 15 we are exposing this GWTCode.hello method and assign it to window.gwtcode_hello. Please note there are two "( )" brackets, first one will declare parameter types and second will take actual parameters. Don't worry if you are using eclipse when you press CTRL + SPACE after method name it will populate corresponding type. Now from java script code you can call window.gwtcode_hello("<name>") method. 

NOTE: If method don't take any argument then line no 15 will look like "return @open.pandurang.client.view.GWTCode::hello()();"

Entry Point class 
Sample.java



package open.pandurang.client.view;

import com.google.gwt.core.client.EntryPoint;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class samples implements EntryPoint {

 public void onModuleLoad() {

  GWTCode.exportMethod();
 }

}

at line no 12 you will see we are calling exportMethod to export the GWTCode Hello method.

html code
sample.html


<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Samples and trials</title>
<script type="text/javascript" language="javascript" src="sample/sample.nocache.js"></script>
</head>
<script type="text/javascript" language="javascript">
 function jsGwtCallTest() {
  var msg = window.gwtcode_hello("Pandurang");
  alert(msg);
 }
</script>
</head>
<body>
    <!-- OPTIONAL: include this if you want history support -->
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position: absolute; width: 0; height: 0; border: 0"></iframe>
    <!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
    <noscript>
        <div
            style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
            Your web browser must have JavaScript enabled in order for this application to display correctly.</div>
    </noscript>
    <button onclick="jsGwtCallTest();">test</button>
</body>
</html>


we are calling this method on event of button click. You will not see any issues here but if you are calling this method from other .js file on some events like on page load or something. You need to make sure gwt entry point has been executed and exported the method.

6 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  2. People often hire computer experts and IT skilled personnel for carrying out data maintaining and record keeping purposes. These experts and skilled workers have an extensive knowledge in computer programming and computer formatting.
    StartPoint

    ReplyDelete
  3. Wow, it seems to me that it is very useful information ! Thanks for sharing such amazing knowledge with other people who learn Java. I also love to share useful information, and wanna suggest everyone to read the explanation about what is a static method in java https://explainjava.com/java-static-methods/ that may help you in some issue.

    ReplyDelete
  4. I just added your weblog to my blog roll, I pray you would take into consideration doing the same. A Blogging Platform for Programmers. You can write your post with markdown.

    ReplyDelete
  5. These agents should be given a break time so that they are able to release some stress and start working again with fresh energy. Even a small cup of tea can work miracles in such conditions.QuickBooks Technical Support

    ReplyDelete