How to call a gwt function when a javascript function is called
I am working on a project on embedding web app on android, following the WebView demo, however I need to call on a gwt function when the javascript function 'wave' is called back by the android app:
<html>
<script language="javascript">
/* This function is invoked by the activity */
function开发者_运维知识库 wave(s) {
// call a gwt function and
// pass 's' to the gwt function
}
</script>
<body>
<!-- Calls into the javascript interface for the activity -->
<a onClick="window.demo.clickOnAndroid()"><div style="width:80px;
...
</div></a>
</body>
</html>
Any ideas on how to achieve this?
You will need to export any method you want to call from javascript into javascript global scope. What this means is that you cannot call an arbitrary java method from hand-written javascript. You must plan ahead and expose the necessary methods in javascript scope.
The process is pretty simple:
- Write a JSNI method that creates a function in $wnd scope.
- From the body of this function call the java method using the JSNI JavaScript to java syntax.
- Call the method declared in step #1 during application startup (for example, from your entry-point onmoduleload)
- Call the function created in $wnd scope from your javascript. Make sure you do this after your gwt module is loaded and entry-point have been run.
An example from GWT JSNI documentation with additional comments:
package mypackage;
public MyUtilityClass
{
//Method to be called from javascript, could be in any other class too
public static int computeLoanInterest(int amt, float interestRate,
int term) { ... }
//This method should be called during application startup
public static native void exportStaticMethod() /*-{
//the function named here will become available in javascript scope
$wnd.computeLoanInterest =
$entry(@mypackage.MyUtilityClass::computeLoanInterest(IFI));
}-*/;
}
EDIT:
Passing parameters to Java method:
When you call Java method that takes parameters, from javascript, you need to use a specific syntax:
[instance-expr.]@class-name::method-name(param-signature)(arguments)
For example, calling a static method that takes a String parameter will look like this:
@com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);
Note that as we are calling a static method, 'instance-expr.' is omitted. The rest of the code is fully qualified class name followed by ::
and method name. The Ljava/lang/String;
after method name, specifies that we need to call the method that takes a String object as parameter. Finally s
is the actual value for that parameter.
Remember that param-signature, Ljava/lang/String;
in our case, in the syntax uses JNI type signature specs, and is required by GWT compiler to select the correct method even if there are multiple overloaded methods with the same name. A param-signature
is required even if the method is not overloaded.
GWT is compiled down to javascript and all function/object names are minified, so they became unreadable and unknown so you can not call them directly from Javascript. To work around this you need to check out how to Call a Java Method from Handwritten JavaScript.
Use Javascript Overlay Types!
Look here:
- Getting to really know GWT, Part 2: JavaScript Overlay Types
- Design: Overlay Types
Can't descib it any better!
精彩评论