Pages

Thursday, April 9, 2009

Java Native Script Interface-2

Share it Please

In my next article of Jsni ,we will be discussing some more points on jsni and I will show you a few more code samples.

Now lets look at an example of accessing the functions defined in the java script to out Gwt page . for this lets modify a html page “Application.html” generated in our Gwt project.

<script type="text/javascript" language="javascript" src="com.example.Application.nocache.js">script>

<script type="text/javascript">

function getFirstName() {

return form.firstName.value;

}

function getLastName(){

return form.lastName.value;

}

</script>

<head>

<body>

<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0">iframe>

<form name="form">

<b>First Nameb>:<input type=text name="firstName" value="">br>

<b>Last Nameb>:<input type=text name="lastName" value="">br>

</form>

</body>

Just copy these both script and body elements into your html file. I have just placed 2 input type elements as firstName and lastName . I have written 2 function’s in java script as getFirstName() and getLastName() which return both the values respectively.So now iam going to access both the methods and display the values that are entered in the html page.

Here Is the code in OnModuleLoad(),

public class Application implements EntryPoint {

//OnModuleLoad

public void onModuleLoad() {

RootPanel.get().add(new Button("Click Me", new ClickListener() {

public void onClick(Widget sender) {

Window.alert(getFirstName());

Window.alert(getLastName());

}

}));

}//OnModuleLoad()

public static native String getFirstName() /*-{

return $wnd.getFirstName();

}-*/;

public static native String getLastName() /*-{

return $wnd.getLastName();

}-*/;

}

In rare cases the methods that are provided by the gwt may not be sufficient for accessing the browser . so we need to write the jsni methods to access the browser window , documents. But the way Gwt loads doesnot allow to directly access the Window and Document . so Gwt provides variables $wnd and $doc for accessing the browser Window and Document as you can see in the above example. In the above Gwt code I have written 2 jsni scripts which access functions defined in java script by using $wnd. $wnd and $doc are representations of java Script Window and Document respectively

Returning Strings , Booleans are simpler from java script to java as they convert directly according to the type..check I have just said to return the values entered.One main we need to take care is when returning “null” value , because null in java script and java are completely different.java script returns “undefined” as a “null” . java script “undefined” is completely differently from java “null” . so we need to manually check whether the value returned from jsni method is null or undefined.unpredictable results can occur if it is not clearly maintained

According to Gwt site, When returning a undefined value from jsni method , the following idiom is to be used

retrun (value==null)?null:value; to avoid returning “undefined”

now lets check another code snippet to acces methods and arguments sent ,

public class Application implements EntryPoint {

public void doSomeThingWithString(String s) {

Window.alert(s);

}

public static void doSomeThingWithInt(int i) {

Window.alert(String.valueOf(i));

}

//OnModuleLoad

public void onModuleLoad() {

RootPanel.get().add(new Button("Click Me", new ClickListener() {

public void onClick(Widget sender) {

doSomeThing("jagadesh",10);

}

}));

}//OnModuleLoad()

public native void doSomeThing(String s,int i)/*-{

@com.example.client.Application::doSomeThingWithInt(I)(i);

this.@com.example.client.Application::doSomeThingWithString(Ljava/lang/String;)(s);

}-*/;

}

Handling Exceptions : it is better to handle exceptions belonging to java script in jsni methods and java exceptions in java code . since when an exception occurred in the jsni method and comes out to java code , the exception becomes one and only “javaScriptException” .so it is always better to use try –catch blocks and use methods to in java script to handle the exception.

No comments :

Post a Comment