解决 Flex模块切换后导致对象转换失败 注册信息丢失
在Flex下, 如果一个项目里面有多个模块时,在第一次加载Module是可以的。在第二次加载module时,“注册”信息会丢失。
即如下的[RemoteClass(alias="com.tjsoft.iess2.po.jxpg.TSysJxUser")]这一行代码;
所产生的错误信息为:
[RPC Fault faultString="Cannot invoke method 'save'." faultCode="Server.ResourceUnavailable" faultDetail="The expected argument types are (com.tjsoft.iess2.po.jxpg.TSysJxUser) but the supplied types were (flex.messaging.io.amf.ASObject) and converted to (null)."]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()[C:/autobuild/3.2.0/frameworks/projects/rpc/src/mx/rpc/AbstractInvoker.as:220]
at mx.rpc::Responder/fault()[C:/autobuild/3.2.0/frameworks/projects/rpc/src/mx/rpc/Responder.as:53]
at mx.rpc::AsyncRequest/fault()[C:/autobuild/3.2.0/frameworks/projects/rpc/src/mx/rpc/AsyncRequest.as:103]
at NetConnectionMessageResponder/statusHandler()[C:/autobuild/3.2.0/frameworks/projects/rpc/src/mx/messaging/channels/NetConnectionChannel.as:569]
at mx.messaging::MessageResponder/status()[C:/autobuild/3.2.0/frameworks/projects/rpc/src/mx/messaging/MessageResponder.as:222]
如下为解决办法:在要调用远程对象的Application文件里面,加上如下声明语句:
import flash.net.registerClassAlias;
import mx.messaging.messages.RemotingMessage;
registerClassAlias("com.tjsoft.iess2.po.jxpg.TSysJxUser", JxUser);
下面是项目中的两个相关文件源代码作为示例
// ActionScript file
package jxpg
{
[Bindable]
[RemoteClass(alias="com.tjsoft.iess2.po.jxpg.TSysJxUser")]
public class JxUser
{
public var id:String;
public var name:String;
public var code:String;
public var createtime:Date;
public var password:String;
public var deptid:String;
public var remark:String;
public var status:String;
public var logintype:String;
public var deptname:String;
}
}
//Application file
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:page="pager.*"
xmlns:dept="jxpg.*"
layout="absolute" verticalAlign="top"
horizontalAlign="center" paddingTop="0"
initialize="initApp()" creationComplete="onCreationComplete()" >
<mx:Style source="skins/blue/blue.css"/>
<mx:Script>
<![CDATA[
import jxpg.JxUser;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import mx.rpc.events.*;
import mx.events.ListEvent;
import mx.managers.PopUpManager;
import jxpg.utils;
import mx.events.CloseEvent;
//申请注册别名,使切换模块后仍然能正常通信
import flash.net.registerClassAlias;
import mx.messaging.messages.RemotingMessage;
registerClassAlias("com.tjsoft.iess2.po.jxpg.TSysJxUser", JxUser); //这里才是重点
}
]]>
</mx:Application>
参考资料1:http://blog.csdn.net/tintown/archive/2009/11/23/4859983.aspx
参考资料2:http://blog.csdn.net/sujun10/archive/2008/08/01/2750977.aspx