Flex-动态调用不同后台数据的方式

一般我们在开发flex应用时,都是定义一个service-config.xml, 然后将blazeds url的信息在这个文件中定义。

<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
           <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
           <properties>
               <polling-enabled>false</polling-enabled>
           </properties>
</channel-definition>

 

然后再编译时指定service-config.xml 文件的位置, flex会自动的进行远程方法调用的绑定。


现在想在运行时指定要访问的后台地址,经过测试是可以实现的。

以下这个flex的Application 就是一个普通的App lication, 没有配置后台服务。

请看代码 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" minWidth="955" minHeight="600"
creationComplete="test();">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.controls.DateField;
import mx.messaging.Channel;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
publicfunction test():void{
var channel:AMFChannel=new AMFChannel("my-amf","http://192.168.101.206:8090/amonitor/spring/messagebroker/amf");
var channelSet:ChannelSet=new ChannelSet();
channelSet.addChannel(channel);
var remoteObject:RemoteObject=new RemoteObject("facade");
remoteObject.channelSet=channelSet;
remoteObject.addEventListener(ResultEvent.RESULT, onResultHandler);
remoteObject.addEventListener(FaultEvent.FAULT, onFaultHandler);
remoteObject.getAlarmColumnInfoList();
}
privatefunction onFaultHandler(event:FaultEvent):void{
Alert.show(event.toString());
}
privatefunction onResultHandler(event:ResultEvent):void{
Alert.show(event.result.toString());
}
]]>
</mx:Script>
</mx:Application>

 

以下是结果,请注意上方的访问方式,是文件方式。

 

经过测试,如果通过url方式的访问,例如将 Test1.html 和 Test1.swf也放到 webapps/amonitor里,然后通过  http://192.168.101.206:8090/amonitor/Test1.html 访问,也是可以的。

但如果放到另外一个web应用里,则会报 2048错误,是因为安全沙箱的问题。
(需要配置跨域访问的策略解决)

配置的方式是  在 206上的 tomcat的  webapps/ROOT  目录下,配置个文件 cross domain.xml
内容如下 

<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" to-ports="*"/>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

这样就可以随意访问了。(要注意安全,细化crossdomain.xml

posted @ 2012-12-14 14:45  小小有  阅读(344)  评论(0)    收藏  举报