论坛首页 Web前端技术论坛

GWT Object Exporter ,GWT模块间传递对象

浏览 11982 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-10-29   最后修改:2009-03-31
GWT

 

GWT Object Exporter<o:p></o:p>

<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 接口,根据需要写出exportimport方法

<o:p> </o:p>

2.1   需要导出的类实现IExportable (或接口继承IExportable)<o:p></o:p>2.2 定义Exporter , 提供 导出/导入 IFace1接口方法<o:p></o:p>

java 代码
  1. interface IFace1  extends IExportable{   
  2.     void method1(String s1);   
  3. }   
  4.   
  5. class Face1 implements IFace1 {   
  6.        public void method1(String s1) {   
  7.            System.out.println("s1 = " + s1);   
  8.     }   
  9. }   

 

 

 

<o:p></o:p>

<o:p>

 

java 代码
  1. interface Exporter extends IExportableExporter {   
  2.   
  3.        public JavaScriptObject doExport(IFace1 iface1);   
  4.   
  5.        public IFace1 doImportIFace1(JavaScriptObject jso);   
  6. }   

 

2.3 应用

<o:p>java 代码</o:p><o:p> </o:p>

 

 

 

  1. public void onModuleLoad() {   
  2.   
  3.     Face1 face1 = new Face1();   
  4.   
  5.     Exporter exporter = (Exporter) GWT.create(Exporter.class);   
  6.   
  7.     JavaScriptObject jso = exporter.doExport(face1);   
  8.   
  9.     //其他模块中:先通过$wnd或其他方式获得此jso,后可导入   
  10.   IFace1 iface1 = exporter.doImportIFace1(jso);    
  11.   
  12.      iface1.method1();   
  13. }   

 

</o:p>

   发表时间:2007-10-30  
gwt提供的1.46包中没有IExportableExporter ,IExportable~~
楼主的这两个接口哪里来的
0 请登录后投票
   发表时间:2007-10-31  
兄弟和这个项目有关系吗?
http://code.google.com/p/gwt-exporter/
0 请登录后投票
   发表时间:2007-10-31  
这是我们公司自己内部的模块,整理后会发布上来的。
0 请登录后投票
   发表时间:2007-11-06  
已经发布了,地址是
http://code.google.com/p/gwt-object-exporter/

包含export模块已经测试类
0 请登录后投票
   发表时间:2007-11-07  
还是少了点类呀~~?
0 请登录后投票
   发表时间:2007-11-07  
o~,粗心大意了,你把错误的方法屏蔽就Ok拉,Download已经更新了~
0 请登录后投票
   发表时间:2007-11-22  
請問這個exporter的作用是什麼?
我想一個html 內有2個gwt 的widget, 2個widget 相互share 一個gwt 的object
可以做到嗎?
請指教
0 请登录后投票
   发表时间:2007-11-23  
可以做到。

原理:
html里面内2个widget可以传递JavaScriptObject.
gwt object expoter就是提供将share的object解析成JavaScriptObject,和导入成类的。
0 请登录后投票
   发表时间:2007-11-28  
我想寫一個GWTSession, 作用就如HTTPSession 一樣, 可以被同一個html 內的gwt widget 互相share object:

GWTSession:
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 session
	GWTSession gwtSession = GWTSessionAccessor.get();
	gwtSession.put("auth", AuthenticationAccessor.get());

	// test the auth has been put into session
	gwtSession = GWTSessionAccessor.get();
	Authentication auth = gwtSession.get("auth");


發現 gwtSession 可以拿出來, 但內部的key/value pair 的數目是0...我用錯了嗎?....謝謝
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics