ResultEvent.result中如果返回的不是java的基本类型则flex会把它转化为Object类型(Object强制转化为As对象后,as对象为null)。如果想让result中的结果自动转化为相应的AS3对象,就需要使用[RemoteClass(alias="完整的java类路径")].
具体的步骤如下:
1 定义java bean
public class SecurityAction {
public String componentId;//组件ID
public boolean isShow;// 有权限为true 否则为false
/**
* 没有权限时的处理方式
* 1 remove:通过调用comp.parent.removeChild()完全删除组件
* 2 visible:将 compl.visible 属性设置为 false。
* 3 enable:将 comp.enable 属性设置为 false。
*/
public String showType;
public String getComponentId() {
return componentId;
}
public void setComponentId(String componentId) {
this.componentId = componentId;
}
public boolean isShow() {
return isShow;
}
public void setShow(boolean isShow) {
this.isShow = isShow;
}
public String getShowType() {
return showType;
}
public void setShowType(String showType) {
this.showType = showType;
}
}
2 定as3对象
[Bindable] [RemoteClass(alias="com.amc.purview.model.SecurityAction")] public class SecurityAction{ public function SecurityAction(){ } public var componentId:String;//组件的id public var isShow:Boolean;//有权限为true 否则为false /** * 没有权限时的处理方式 * 1 remove:通过调用comp.parent.removeChild()完全删除组件 * 2 visible:将 compl.visible 属性设置为 false。 * 3 enable:将 comp.enable 属性设置为 false。 */ public var showType:String; }
3. java端返回结果。
public ArrayCollection getActions(String userId,String pageId){
ArrayCollection ac=new ArrayCollection();
SecurityAction action=new SecurityAction();
action.setComponentId("btn2");
action.setShow(false);
action.setShowType("enable");
ac.add(action);
return ac;
}
4 flex端处理返回结果。
private function getActionSuc(result:ResultEvent):void{ var actions:ArrayCollection=result.result as ArrayCollection; if(actions!=null){ Security.load(actions,comp); } }
注意:1 as3对象和java对象的属性必须一致,public和private都可以的。
2 按照规范,暴露给Flex调用的不能包含以下保留的方法名:
disconnect()
getOperation()
hasOwnProperty()
initialized()
isPrototypeOf()
logout()
propertyIsEnumerable()
setCredentials()
setPropertyIsEnumerable()
setRemoteCredentials()
toString()
valueOf()
如果不小心用到了以上的方法,可以用以下方法解决。
public var myRemoteObject:RemoteObject = new RemoteObject();
myRemoteObject.destination = "ro-catalog";
public var op:Operation = myRemoteObject.getOperation("hasOwnProperty");
注意:如果flex中有多个Module,就有可能导致RemoteClass信息丢失,如何解决RemoteClass信息丢失,请参考我的另一篇博客解决RemoteClass alias信息丢失。