Flex 处理服务器端传来的数据第1/2页
更新时间:2009年08月09日 00:43:18 作者:
对于Java端返回的各种Java类型的对象,Flex中能否有相应的数据类型来映射。这是Flex与服务器通信使用remoteObject的关键。
下面我举个较为全面的例子,先看整个项目的结构,服务器端使用BlazeDs,结构为
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:RemoteObject destination="helloService"
id="test1">
<mx:method name="hello"
result="sayHelloResult(event)"/>
<mx:method name="world"
result="sayWorldResult(event)"/>
<mx:method name="getMap"
result="getMapResult(event)"/>
<mx:method name="getList"
result="getListResult(event)"/>
<mx:method name="getSet"
result="getSetResult(event)"/>
</mx:RemoteObject>
<mx:HBox>
<mx:Label text="输入:"/>
<mx:TextInput id="inputStr"/>
<mx:Button label="say hello"
click="sayHello(event);"/>
<mx:Button label="say world"
click="sayWorld(event);"/>
<mx:Button label="get map"
click="test1.getMap()"/>
<mx:Button label="get list"
click="test1.getList()"/>
<mx:Button label="get set"
click="test1.getSet()"/>
</mx:HBox>
<mx:HBox>
<mx:Label text="结果:"/>
<mx:TextArea id="result"/>
</mx:HBox>
<mx:Script>
<!--[CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import cn.Person;
[Bindable]
public var person:Person;
[Bindable]
public var map:ArrayCollection=null;
function sayHello(event:Event):void
{
var inputVar:String=inputStr.text;
test1.hello(inputVar);
}
function sayWorld(event:Event):void
{
var inputVar:String=inputStr.text;
test1.world(inputVar);
}
private function sayHelloResult(event:ResultEvent):void
{
result.text=event.result.toString();
Alert.show(event.result.toString(), "返回结果");
}
private function sayWorldResult(event:ResultEvent):void
{
result.text=event.result.toString();
Alert.show(event.result.toString(), "返回结果");
}
private function getMapResult(event:ResultEvent):void
{
for (var key:String in event.result)
{
var list:ArrayCollection=event.result[key] as ArrayCollection;
for (var key1:Object in list)
{
Alert.show("map item key is: " + key + " value is: " + key1);
}
}
}
private function getList():void
{
test1.getList();
}
private function getListResult(event:ResultEvent):void
{
var ac:ArrayCollection=event.result as ArrayCollection;
for (var i:int=0; i < ac.length; i++)
{
var p1:Person=ac.getItemAt(i) as Person;
Alert.show("list item name is: " + p1.name+" \n another usage : "+ac.getItemAt(i).name);
}
}
private function getSet():void
{
test1.getSet();
}
private function getSetResult(event:ResultEvent):void
{
var ac:ArrayCollection=event.result as ArrayCollection;
for (var i:int=0; i < ac.length; i++)
{
var p1:Person=ac.getItemAt(i) as Person;
Alert.show("set item name is: " + p1.name);
}
}
]]-->
</mx:Script>
</mx:Application>
客户端使用flex builder 3开发,结构就遵循flex builder 3标准
client.mxml为
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:RemoteObject destination="helloService"
id="test1">
<mx:method name="hello"
result="sayHelloResult(event)"/>
<mx:method name="world"
result="sayWorldResult(event)"/>
<mx:method name="getMap"
result="getMapResult(event)"/>
<mx:method name="getList"
result="getListResult(event)"/>
<mx:method name="getSet"
result="getSetResult(event)"/>
</mx:RemoteObject>
<mx:HBox>
<mx:Label text="输入:"/>
<mx:TextInput id="inputStr"/>
<mx:Button label="say hello"
click="sayHello(event);"/>
<mx:Button label="say world"
click="sayWorld(event);"/>
<mx:Button label="get map"
click="test1.getMap()"/>
<mx:Button label="get list"
click="test1.getList()"/>
<mx:Button label="get set"
click="test1.getSet()"/>
</mx:HBox>
<mx:HBox>
<mx:Label text="结果:"/>
<mx:TextArea id="result"/>
</mx:HBox>
<mx:Script>
<!--[CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import cn.Person;
[Bindable]
public var person:Person;
[Bindable]
public var map:ArrayCollection=null;
function sayHello(event:Event):void
{
var inputVar:String=inputStr.text;
test1.hello(inputVar);
}
function sayWorld(event:Event):void
{
var inputVar:String=inputStr.text;
test1.world(inputVar);
}
private function sayHelloResult(event:ResultEvent):void
{
result.text=event.result.toString();
Alert.show(event.result.toString(), "返回结果");
}
private function sayWorldResult(event:ResultEvent):void
{
result.text=event.result.toString();
Alert.show(event.result.toString(), "返回结果");
}
private function getMapResult(event:ResultEvent):void
{
for (var key:String in event.result)
{
var list:ArrayCollection=event.result[key] as ArrayCollection;
for (var key1:Object in list)
{
Alert.show("map item key is: " + key + " value is: " + key1);
}
}
}
private function getList():void
{
test1.getList();
}
private function getListResult(event:ResultEvent):void
{
var ac:ArrayCollection=event.result as ArrayCollection;
for (var i:int=0; i < ac.length; i++)
{
var p1:Person=ac.getItemAt(i) as Person;
Alert.show("list item name is: " + p1.name+" \n another usage : "+ac.getItemAt(i).name);
}
}
private function getSet():void
{
test1.getSet();
}
private function getSetResult(event:ResultEvent):void
{
var ac:ArrayCollection=event.result as ArrayCollection;
for (var i:int=0; i < ac.length; i++)
{
var p1:Person=ac.getItemAt(i) as Person;
Alert.show("set item name is: " + p1.name);
}
}
]]-->
</mx:Script>
</mx:Application>
相关文章
使用asx3m与xstream配合解决flex与java利用httpservice传递xml数据问题
过年期间抽空在弄flex前端对象与后端java对象利用httpservice交互的问题。比如前端需要user list数据,构造了flex user object类。2009-02-02Flex与.NET互操作(十二):FluorineFx.Net的及时通信应用(Remote Shared Objects
远程共享对象(Remote Shared Objects) 可以用来跟踪、存储、共享以及做多客户端的数据同步操作。只要共享对象上的数据发生了改变,将会把最新数据同步到所有连接到该共享对象的应用程序客户端。2009-06-06
最新评论