网上找了一些关于flex与java互通的文章,貌似都很老的,并且不适合我个人使用,主要是配置太繁琐(基于AMF3),不过也还是找到一些资料自己加以实践 修改还是搞出点东西来了.
本文实例中,客户端读取远程数据本地生成导航栏及下拉菜单.
etmvc
1 下载 flex-messaging-common.jar 和 flex-messaging-core.jar加入到path中
2 扩展etmvc的视图以支持AMF3(关于 etmvc的视图 http://www.etmvc.cn/tutorial/show/15)
AMF3View.java
/* ***********************************************
* author : Anyhome
* email : [email protected]
* function:
* @date 2009-10-30
* history: created by Anyhome
* ***********************************************/
package org.anyhome;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.et.mvc.View;
import com.et.mvc.ViewRendererClass;
@ViewRendererClass(AMF3ViewRenderer.class)
public class AMF3View extends View {
private Object amf3pojo;
public AMF3View(Object amf3pojo){
//this.amf3pojo = PojoToMap(amf3pojo);
this.amf3pojo = amf3pojo;
}
/**
* @param amf3pojo the amf3pojo to set
*/
public void setAmf3pojo(Object amf3pojo) {
this.amf3pojo = amf3pojo;
}
/**
* @return the amf3pojo
*/
public Object getAmf3pojo() {
return amf3pojo;
}
}
AMF3ViewRenderer.java
/* ***********************************************
* author : Anyhome
* email : [email protected]
* function:
* @date 2009-10-30
* history: created by Anyhome
* ***********************************************/
package org.anyhome;
import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.util.zip.DeflaterOutputStream;
import com.et.mvc.ViewContext;
import com.et.mvc.renderer.AbstractViewRenderer;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;
public class AMF3ViewRenderer extends AbstractViewRenderer<AMF3View> {
@Override
protected void renderView(AMF3View viewObject, ViewContext viewContext)
throws Exception {
SerializationContext context = new SerializationContext();
Amf3Output out = new Amf3Output(context);
DeflaterOutputStream stream = new DeflaterOutputStream(new DataOutputStream(viewContext.getResponse().getOutputStream()));
out.setOutputStream(stream);
out.writeObject(viewObject.getAmf3pojo());
stream.finish();
}
}
3 写一个POJO转MAP的方法
public AMF3View List(int id) throws ActiveRecordException{
List<MyModule> myModule = MyModule.findAll(MyModule.class,"M_ParentID=?",
new Object[]{id});
List<Map> lst = new ArrayList<Map>();
for(MyModule items:myModule)
{
lst.add(PojoToMap(items));
}
return new AMF3View(lst);
}
4 调用
public AMF3View List(int id) throws ActiveRecordException{
List<MyModule> myModule = MyModule.findAll(MyModule.class,"M_ParentID=?",
new Object[]{id});
List<Map> lst = new ArrayList<Map>();
for(MyModule items:myModule)
{
lst.add(PojoToMap(items));
}
return new AMF3View(lst);
}
flex
public function GetModGroupLst():void{
var myRequest:URLRequest = new URLRequest( org.System.ProxyHost + "Aysuite2/AppManager/Module/List/0");
var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, loadSuccessful);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, loadError);
myLoader.load(myRequest);
myLoader.dataFormat=URLLoaderDataFormat.BINARY;
}
private function loadSuccessful(e:Event):void { var byte:ByteArray = e.target.data as ByteArray; byte.uncompress(); var obj:Object = byte.readObject(); for(var a:Object in obj){ var but:AButton = new AButton(); but.label=obj[a].getM_CName; but.id=obj[a].getModuleID; but.name=obj[a].getM_Directory; //getM_Directory 对于我的 POJO中的 getM_Directory but.addEventListener(MouseEvent.CLICK,flipTonav); bottomlayout.addChild(but); } } private function loadError(e:IOErrorEvent):void { Alert.show("网络错误"); }