如何使用registerClassAlias来解决module中使用RemoteObject---第一次访问正常、第二次出错...

  问题:今天我在Flex的module中使用RemoteObject的方式与服务端进行AMF的远程数据调用。第一次切换到module时是没有问题的,但第二次切换到module时,发现总是报错,无法完成远程调用。为此困惑极了。
  我在remoting访问时,使用了一个自定义的参数类:
  package VO { [RemoteClass(alias="ServiceLibrary.SystemFramework .UserInfoSearchCondition")] public class UserInfoSearchConditionVO { public function UserInfoSearchConditionVO() { } public var UserName:String; public var DepartmentId:String; } } 这个定义里使用到了:[RemoteClass(alias="ServiceLibrary.SystemFramework .UserInfoSearchCondition")]
  这就是本地对象与"远程服务端类"的别名定义,这个大家都应该知道的。
  远程调用时:
  var condition:UserInfoSearchConditionVO=new UserInfoSearchConditionVO(); condition.UserName=txtSearchUserName.text; var resp1:Responder=new Responder(onUserSearchCountSuccess,onUserSearchCou ntFaild); nc.call("ServiceLibrary.SystemFramework.UserInfoFa cade.GetUserInfoCountByCondition",resp1,condition); 但不知道为什么,在第一次Module时加载是可以的。在第二次加载module时,此"注册"信息会丢失。
  解决方法:需要明确声明此注册信息,代码如下: import flash.net.registerClassAlias; import mx.messaging.messages.RemotingMessage; import VO.UserInfoSearchConditionVO; registerClassAlias("ServiceLibrary.SystemFramework .UserInfoSearchCondition", UserInfoSearchConditionVO); 以上的代码可以放在自己的module里,或者也可以直接放在Application上,声明一次也就可以了,子Module不需要更申明了。
  最后的代码我帖出来,如果你能看懂就最好了,看不懂我也没办法了:)
   flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" layout="vertical" width="100%" height="100%" creationComplete="module1_creationCompleteHandler( event)" > 0) this.btnPrev.visible=true; else this.btnPrev.visible=false; if((curPage+1) * pageCount >=totalCount) this.btnNext.visible=false; else this.btnNext.visible=true; } protected function onUserSearchCountFaild(re:String):void{ Alert.show("获取数据出错!","出错了"); } protected function onUserSearchSuccess(re:ArrayCollection):void{ gridUser.dataProvider =re; } protected function onUserSearchFaild(re:String):void{ Alert.show("获取数据出错!","出错了"); } protected function btnNext_clickHandler(event:MouseEvent):void { // TODO Auto-generated method stub curPage++; BindGrid(); } protected function btnPrev_clickHandler(event:MouseEvent):void { // TODO Auto-generated method stub curPage--; BindGrid(); } protected function gridUser_itemClickHandler(eventistEvent):void { // TODO Auto-generated method stub this.currentState="edit"; } ]]--> 最后,我再主张一下我的设计的原则:
  在客户端与服务端进行远程数据调用时,我不太主张采用"简单参数"模式,比如查询用户功能吧,我可以使用username,deparmentId...等等参数来进行参数调用,但这样有个扩展性问题:那就是当以后要添加"查询条件"时,不得不修改接口。这就是会导致客户端要修改N个调用地方,服务端也要进行接口修改,这就会带来很大的扩展问题。
  因此,我建议可以自定义一个"Condition查询类"与"服务端"的相对应,这样接口的参数就只需要一个,而且类型不变。
  以后如果要扩展"查询条件"时,只需要在"客户端"添加一个属性,并赋值就OK了。在"服务端"则也只要添加属性,并修改实现代码就OK了,不再需要修改两端的"接口"了。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值