Accessing GWT class from external JS loaded in IFrame

67 views
Skip to first unread message

Jurjen van Geenen

unread,
Sep 8, 2011, 3:22:35 PM9/8/11
to gwtexporter
Hi,

I'm creating an IFrame within GWT which is loaded with a JSP
containing the sun deployJava script, which in turn loads an applet.
The applet has to access a GWT class (through the Java Plugin/
JavaScript interface). Without the frame, I know how to do it, but it
has to be there and created within GWT for several reasons. How should
I export this GWT class and how can I access it from external
JavaScript within the IFrame?

Thanks,

Jurjen

Manuel Carrasco Moñino

unread,
Sep 8, 2011, 4:05:39 PM9/8/11
to gwtex...@googlegroups.com
When you export a class or a method with gwtexporter, it is available in the main window object, for instance:

@ExportPackage("mypackage")
@Export("myclass")
class AClass implements Exportable {
  @Export("method")
  public String aMethod() {
    return "hello"
  }
}

var c = new mypackage.myclass();
alert(c.method());

So if you have created an iframe, and the iframe content comes from the same domain (otherwise it is no possible due to security reasons), you should be able to access to parent methods:

var c = new parent.window.mypackage.myclass();
alert(c.method());


- Manuel Carrasco Moñino

Jurjen van Geenen

unread,
Sep 10, 2011, 9:07:17 AM9/10/11
to gwtex...@googlegroups.com
Hi Manuel,

Thanks for your quick reply. I did what you suggest below, unfortunately no succes. It seems the exporter is not doing anything: reading another JavaScript variable declared in the main window from within the frame does work however. The error I get in JavaScript upon "var c = new mypackage.myclass();" in my frame's script is ''Uncaught TypeError: Cannot read property 'myclass' of undefined". I'm using your latest snapshot (maven) and GWT 2.3. Besides the code you suggest I have the following lines before main module dec in my gwt.xml:

  <inherits name='org.timepedia.exporter.Exporter'/>
  <set-property name="export" value="yes"/>

I also tried static exports to $wnd, no luck theire either. Am I missing something?

Thanks,

Jurjen

Manuel Carrasco Moñino

unread,
Sep 10, 2011, 1:12:26 PM9/10/11
to gwtex...@googlegroups.com
could you send the code you are testing with?

Jurjen van Geenen

unread,
Sep 10, 2011, 3:34:30 PM9/10/11
to gwtex...@googlegroups.com
Hi Manuel,

Sure I attached a simple Maven project created with the GWT Maven archetype
(you need to build it so the async stuff is generated). I looked at the ExporterDemo module in your project's demo's and almost copy pasted, but it's not working.

Many thanks,

Jurjen
simple.zip

Manuel Carrasco Moñino

unread,
Sep 11, 2011, 12:56:16 AM9/11/11
to gwtex...@googlegroups.com
The problem is that AClass is not public, so exportAll will not export the class, it will be only exported if you call GWT.create() or the class were used in a method, but it would be exported without any public constructor and you could not instantiate a new object from js, replace
class AClass implements Exportable {
by
public class AClass implements Exportable {

Another small problem in your code is a typo: in js you are calling aMethod() and bMethod() instead of hello().


- Manolo

Jurjen van Geenen

unread,
Sep 11, 2011, 4:07:50 AM9/11/11
to gwtex...@googlegroups.com
Thank you so much for your help and this great project: its working now!

Best regards,

Jurjen
Reply all
Reply to author
Forward
0 new messages