出家如初,成佛有余

Flex 对Java端返回Collection的处理方法

Posted in Uncategorized by chuanliang on 2009/07/22

    将Flex与Spring集成后(BlazeDS 与Spring集成指南 ),第一个面临的问题就是:对于Java端返回的各种Java类型的对象,Flex中能否有相应的数据类型来映射。

处理,尤其是List、Set、Map及POJO对象值。

    在 BlazeDS 与Spring集成指南 例子的基础上,调整相关的测试代码如下:

1、Java端

1.1、com.yeeach.HelloWorldService 

package com.yeeach;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HelloWorldService {
    public String hello(String var1) {
        return "hello " + var1;
    }
    public String world(String var1) {
        return "world " + var1;
    }
    public Map<String,List<String>> getMap() {
        ArrayList<String> list1=new ArrayList<String>();
        list1.add("value11");
        list1.add("value12");
        list1.add("value13");
        ArrayList<String> list2=new ArrayList<String>();
        list2.add("value21");
        list2.add("value22");
        list2.add("value23");
        ArrayList<String> list3=new ArrayList<String>();
        list3.add("value31");
        list3.add("value32");
        list3.add("value33");       
        HashMap<String,List<String>> map=new HashMap<String,List<String>>();
        map.put("key1", list1);
        map.put("key2", list2);
        map.put("key3", list3);       
        return map;
    }
    public ArrayList<Person> getList() {
        ArrayList <Person> list=new ArrayList<Person>();
        Person p1=new Person();
        p1.setName("name1");
        p1.setPass("pass1");
        Person p2=new Person();
        p2.setName("name2");
        p2.setPass("pass2");
        Person p3=new Person();
        p3.setName("name3");
        p3.setPass("pass3");
        list.add(p1);
        list.add(p2);
        list.add(p3);
        return list;
    }
    public Set<Person> getSet() {
        HashSet<Person>  set=new HashSet<Person>();
        Person p1=new Person();
        p1.setName("name1");
        p1.setPass("pass1");
        Person p2=new Person();
        p2.setName("name2");
        p2.setPass("pass2");
        Person p3=new Person();
        p3.setName("name3");
        p3.setPass("pass3");
        set.add(p1);
        set.add(p2);
        set.add(p3);
        return set;
    }   
}

1.2、com.yeeach.Person

package com.yeeach;

public class Person {

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPass() {
            return pass;
        }
        public void setPass(String pass) {
            this.pass = pass;
        }
        private String name;
        private String pass;

}

2、Flex 端

2.1、helloworld.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                layout="vertical">
    <mx:RemoteObject destination="test1"
                     id="test1">
        <mx:method name="hello"
                   result="sayHelloResult(event)"/>
        <mx:method name="world"
                   result="sayWorldResult(event)"/>
        <mx:method name="getMap"
                   resul
t="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 com.yeeach.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++)
 &#16
0;              {
                    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>

2.2、com.yeeach.Person.as

package com.yeeach
{
    [Bindable]
    [RemoteClass(alias="com.yeeach.Person")]
    public class Person
    {
        public var name:String;
        public var pass:String;
    }
}

 

 

3、总结:

    1、JavaBean从Java端返回给Flex,可以通过Flex的Meta Tag [RemoteClass (alias=” “)]来标识服务器端对应的Java的类(一般为POJO对象)。

注意位置,是在class声明前。

    2、Java Collection返回给Flex,都可以通过转化为mx.collections.ArrayCollection来进行处理。

    3、Flex对于服务器端返回的java其他类型的处理,相对简单,可以参考

http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_4.html

No Responses Yet

Subscribe to comments with RSS.

  1. 小米 said, on 2009/07/28 at 10:46

    谢谢你的文章!
    请问:我如果只要ac 的值,
    那么getListResult event:ResultEvent)中的for 循环还用写吗?

  2. 小米 said, on 2009/07/31 at 09:43

    Alert.show(“list item name is: ” p1.name ” \n another usage : ” ac.getItemAt(i).name);

    ac.getItemAt(i).name 能出来吗?

  3. […] 原文地址:Flex 对Java端返回Collection的处理方法 […]

  4. HuRong said, on 2010/01/18 at 16:38

    谢谢你的文章,让我解决一个头疼的问题


发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: