GWT Object Exporter ,GWT模块间传递对象
?
GWT Object Exporter<o:p></o:p>
1.??????? 简介<o:p>?</o:p>
GWT 允许传递的对象为Element,JavaScriptObject<o:p>?</o:p>
GWT Object Exporter 提供将页面对象 导出(export) 成JavaScriptObject,将JavaScriptObject 导入(import)成代理对象(服务)
<o:p>?</o:p>
2.??????? 核心 & Demo<o:p></o:p>
interface IExportable????? :??? 实现此接口的类将允许exporter 成 javaScriptObject
interface IExportableExporter?????? :??? exporter 接口,根据需要写出export、import方法
<o:p>?</o:p>
2.1?? 需要导出的类实现IExportable (或接口继承IExportable)<o:p></o:p>2.2 定义Exporter , 提供 导出/导入 IFace1接口方法<o:p></o:p>
??
?
<o:p></o:p>
<o:p>?
?2.3 应用
<o:p>java 代码</o:p><o:p> </o:p>
?
?
?
package commons.gwt.data.client;import java.util.HashMap;import com.macaufly.gwt.exporter.client.IExportable;public class GWTSession extends HashMap implements IExportable {}package commons.gwt.data.client;import com.google.gwt.core.client.JavaScriptObject;import com.macaufly.gwt.exporter.client.IExporter;public interface GWTSessionHelper extends IExporter {public JavaScriptObject doExport(GWTSession gwtSession);public GWTSession doImport(JavaScriptObject jso);}
package commons.gwt.data.client;import com.google.gwt.core.client.GWT;import com.google.gwt.core.client.JavaScriptObject;public class GWTSessionAccessor {private static GWTSessionHelper helper = (GWTSessionHelper) GWT.create(GWTSessionHelper.class);public static void clear() {setGWTSession(null);}private static native void setGWTSession(JavaScriptObject gwtSession) /*-{$wnd.parent.gwtSession = gwtSession;}-*/;public static native JavaScriptObject getGwtSession() /*-{if ($wnd.parent.gwtSession == undefined) {return null;} else {return $wnd.parent.gwtSession}}-*/;public static GWTSession get() {JavaScriptObject jso = getGwtSession();if (jso == null) {GWTSession gwtSession = new GWTSession();setGWTSession(helper.doExport(gwtSession));return gwtSession;} else {GWTSession gwtSession = helper.doImport(jso);return gwtSession;}}}
// put the auth into sessionGWTSession gwtSession = GWTSessionAccessor.get();gwtSession.put("auth", AuthenticationAccessor.get());// test the auth has been put into sessiongwtSession = GWTSessionAccessor.get();Authentication auth = gwtSession.get("auth");
public static native JavaScriptObject getGwtSession() /*-{if ($wnd.parent.gwtSession == undefined) {return null;} else {return $wnd.parent.gwtSession}}-*/;
GWTSession gwtSession = helper.doImport(jso);
package commons.gwt.data.client;import java.util.HashMap;import com.macaufly.gwt.exporter.client.IExportable;public class GWTSession extends HashMap implements IExportable {private Object objectpublic Object getObject() {return object;}public void setObject(Object object) {this.object = object;}}
public class TestExportable implements EntryPoint{public void onModuleLoad() {exporter e = (exporter) GWT.create(exporter.class);GWTSession session = new GWTSession();session.put("auth", new A("auth"));session.put("2", new String("adf"));JavaScriptObject jso = e.export(session);GWTSession session2 = e.impor(jso);System.out.println(((A)session2.get("auth")).getN());}class A implements IExportable{String n;public A(String n2) {n = n2;// TODO Auto-generated constructor stub}public String getN(){return n;}}interface exporter extends IExporter{JavaScriptObject export(GWTSession session);GWTSession impor(JavaScriptObject jso);}}
java.lang.ClassCastException: commons.gwt.security.client.Authentication cannot be cast to commons.gwt.security.client.Authentication
Window.alert("objectTest getCredential -> " + ((Authentication) AuthenticationHelper.helper.doImport((JavaScriptObject) GWTSessionAccessor.get().get("objectTest"))).getCredential());
[ERROR] Unable to load module entry point class myFrameTest.inner.client.Inner (see associated exception for details)java.lang.ClassCastException: com.google.gwt.core.client.JavaScriptObject cannot be cast to com.google.gwt.core.client.JavaScriptObjectat myFrameTest.inner.client.Inner.onModuleLoad(Inner.java:27)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:342)at com.google.gwt.dev.shell.BrowserWidget.attachModuleSpace(BrowserWidget.java:326)at com.google.gwt.dev.shell.ie.BrowserWidgetIE6.access$200(BrowserWidgetIE6.java:36)at com.google.gwt.dev.shell.ie.BrowserWidgetIE6$External.gwtOnLoad(BrowserWidgetIE6.java:70)at com.google.gwt.dev.shell.ie.BrowserWidgetIE6$External.invoke(BrowserWidgetIE6.java:125)at com.google.gwt.dev.shell.ie.IDispatchImpl.Invoke(IDispatchImpl.java:293)