javascript calling java

51 views
Skip to first unread message

Luiz Nunes

unread,
Dec 13, 2006, 8:39:23 PM12/13/06
to Google-We...@googlegroups.com
I need to call a java method from a onclick link.

<a href="#" onclick="a()"></a>


public class A extends Composite {

public static native void a() -/* {
// ...
} -*/;

}


doesn't work.
any help?

thank you.

Mat Gessel

unread,
Dec 13, 2006, 9:31:01 PM12/13/06
to Google-We...@googlegroups.com
Hi Luiz,

If I understand correctly, you want to call a GWT method from a plain,
non-GWT "A" tag. To do this there are two things you need to know:

1) the compiled GWT script runs in an embedded iframe in the host
page, therefore GWT runs in different scope than JavaScript in host
page. You *can* access external JavaScript entities from GWT via the
$wnd variable. However, you cannot (AFAIK) access the embedded GWT
script from external scripts (read on for workaround).

2) the GWT script is obfuscated. Even if you could call into the GWT
script, you would find that the method name has been changed. GWT does
not provide a way to selectively disable obfuscation. You could
compile with the -PRETTY option to prevent the entire script from
being obfuscated, but this results in a considerably larger download.

The solution:
GWT can access the external JavaScript scope via $wnd in a JSNI
method. You can define a function on $wnd which calls your GWT method.
Your GWT method name will be obfuscated in the compiled script, but
this will not be a problem: the GWTCompiler parses your JSNI code and
updates references to obfuscated methods.

You can see this technique in action in the GWT Tk Debug demo. Click
on the "regular JavaScript" link.
http://www.asquare.net/gwttk/apps/demo/Demo.html#debug

HTML source:
<a href='#' onclick="Debug.println('This is a pure HTML onclick
handler');return false;">regular JavaScript</a>

GWT Source:
http://gwt-tk.googlecode.com/svn/tk/trunk/src/java/asquare/gwt/debug/client/Debug.java

--
Mat Gessel
http://www.asquare.net/gwttk/

Luiz Nunes

unread,
Dec 14, 2006, 8:23:19 PM12/14/06
to Google-We...@googlegroups.com
It work perfectly.
Thank you...

2006/12/13, Mat Gessel <mat.g...@gmail.com>:

mithrandir

unread,
Dec 16, 2006, 6:27:47 AM12/16/06
to Google Web Toolkit
Hi!

I tried it and it works fine for static methods. Is there a way to get
this working with non-static methods? E.g. clicking on a html link
calls a nonstatic method that runs an rpc call and updates widgets
afterwards.

> GWT Source:
> http://gwt-tk.googlecode.com/svn/tk/trunk/src/java/asquare/gwt/debug/client/Debug.java

in your jsInit() i found this line: if ($wnd.Debug === undefined)
Is this intentionally?

Mat Gessel

unread,
Dec 16, 2006, 3:51:43 PM12/16/06
to Google-We...@googlegroups.com
On 12/16/06, mithrandir <mithr...@gmx.li> wrote:
> I tried it and it works fine for static methods. Is there a way to get
> this working with non-static methods? E.g. clicking on a html link
> calls a nonstatic method that runs an rpc call and updates widgets
> afterwards.

If the GWT method is not static, then it cannot be known at compile
time. You would need to create a facility with which the GWT object
can let the native object know the GWT object is interested. It could
register in a look up table or set a callback. I'm not sure about
this, but you may have to clean up references to prevent memory leaks
in IE.

You may want to consider putting your content into an HTMLPanel and
using a SimpleHyperLink to trigger the action.
http://gwt-tk.googlecode.com/svn/tk/trunk/src/java/asquare/gwt/tk/client/ui/SimpleHyperLink.java

Another possibility is the Wrapper classes in GWT WL:
http://gwt-widget.sourceforge.net/docs/apidocs/org/gwtwidgets/client/wwrapper/package-summary.html

> > GWT Source:
> > http://gwt-tk.googlecode.com/svn/tk/trunk/src/java/asquare/gwt/debug/client/Debug.java
>
> in your jsInit() i found this line: if ($wnd.Debug === undefined)
> Is this intentionally?

Yes. (typeof $wnd.Debug == "undefined") would probably work too,
though the expression in question seems cleaner to me.

mithrandir

unread,
Dec 17, 2006, 6:31:08 AM12/17/06
to Google Web Toolkit
Thanks for responding so fast Mat.

the problem is, I am using dynamically generated image maps. I think
its less complicated to use a HTML Panel, fill it with image and map
tags and use document.getElementById('id').click() as hook back to gwt.


<area shape="rect"
href="javascript:document.getElementById('gwtButton').click();" ...

where gwtButton is the id of a GWT Button Widget that has a
eventlistener which fires the rpc

Reply all
Reply to author
Forward
0 new messages