读书人

GWT Object Exporter ,GWT模块间传接对

发布时间: 2012-11-23 22:54:33 作者: rapoo

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 {}

然需要Helper 去import/export:
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);}


了GWT Widget 可以方便拿到GWTSession用, 了一GWTSessionAccessor
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");


gwtSession 可以拿出, 但部的key/value pair 的目是0...我用了?....private static native void setGWTSession(JavaScriptObject gwtSession) /*-{$wnd.parent.gwtSession = gwtSession;}-*/;

另一模块可以利html 的javascript 拿回GWTSession 的jso
public static native JavaScriptObject getGwtSession() /*-{if ($wnd.parent.gwtSession == undefined) {return null;} else {return $wnd.parent.gwtSession}}-*/;


import 成GWTSession
GWTSession gwtSession = helper.doImport(jso);


在於,在第一模块put 入GWTSession 的key/value pair, 在第二模块中拿不出黎, 全部是null

故把GWTSession 改成:
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;}}


第一模块 setObject(new String("test")), 在第二模块可以拿到"test", 是否exporter object 有一定要限制? 如必pojo, 因我一只有private constructor 的class 不能import/export.public class GWTSession implements IExportable{HashMap map = new HashMap();public Object get(Object key) {return map.get(key);}public Object put(Object key, Object value) {return map.put(key, value);}public int size() {return map.size();}}
之前不ok的原因是传递后的GWTSession实际上创建了新的HashMap.

ok,简单对象也是可以传递的,如下:
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);}}

测试通过,A也要实现IExpotable.
java.lang.ClassCastException: commons.gwt.security.client.Authentication cannot be cast to commons.gwt.security.client.Authentication


奇怪的是, 如果"stringTest"/"String" 和 "objectTest"/Authentication 2 key/value 同是在第二模块中入和出的, 就可以利取

教一下[ERROR] Unable to load module entry point class myFrameTest.inner.client.Inner (see associated exception for details)java.lang.ClassCastException: myFrameTest.commons.client.Authentication cannot be cast to myFrameTest.commons.client.Authenticationat myFrameTest.inner.client.Inner.onModuleLoad(Inner.java:22)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)gwtSession.put("objectTest", AuthenticationHelper.helper.doExport(auth));

取出:
Window.alert("objectTest getCredential -> " + ((Authentication) AuthenticationHelper.helper.doImport((JavaScriptObject) GWTSessionAccessor.get().get("objectTest"))).getCredential());


次也是ClassCastException, 但成 JavaScriptObject
[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)


Orz... 22 楼 yongyuan.jiang 2007-11-29 ok了。

public class GWTSession implements IExportable {

HashMap map = new HashMap();

public Object putA(String key,Authentication value){
return map.put(key, value);
}

public Authentication getA(Object key) {
return (Authentication) map.get(key);
}
}

需要显示写出该类,exporter才会自动将他export和import. 23 楼 yongyuan.jiang 2007-11-29 session.putA(new Authentication ());

session.getA("key");
24 楼 kelvinlaw 2007-11-29 主的耐心指, 已能成功互相share 了, 然必在GWTSession 定所有可能要放入map 的class 的put /get method, 有不便, 但至少足了GWTSession 有的功能

了一下, put 不用另行putA, 用原本的put(String key, Object value) 已可以, 但必用public Authentication getA(String key)取出

gwt-object-exporter 的出比上以serialize/deserialize 的方法去share object between widget , 在易用性, 效率, 和即同步性都在好太多!

仍然期待主的用示.

读书人网 >Web前端

热点推荐