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

 

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

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

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

 1 1、Java端

 2 1.1、com.yeeach.HelloWorldService  

 3 package com.yeeach; 

 4 

 5 import java.util.ArrayList; 

 6 import java.util.HashMap; 

 7 import java.util.HashSet; 

 8 import java.util.List; 

 9 import java.util.Map; 

10 import java.util.Set; 

11 

12 public class HelloWorldService { 

13     public String hello(String var1) { 

14         return "hello " + var1; 

15     } 

16     public String world(String var1) { 

17         return "world " + var1; 

18     } 

19     public Map<String,List<String>> getMap() { 

20         ArrayList<String> list1=new ArrayList<String>(); 

21         list1.add("value11"); 

22         list1.add("value12"); 

23         list1.add("value13"); 

24         ArrayList<String> list2=new ArrayList<String>(); 

25         list2.add("value21"); 

26         list2.add("value22"); 

27         list2.add("value23"); 

28         ArrayList<String> list3=new ArrayList<String>(); 

29         list3.add("value31"); 

30         list3.add("value32"); 

31         list3.add("value33");        

32         HashMap<String,List<String>> map=new HashMap<String,List<String>>(); 

33         map.put("key1", list1); 

34         map.put("key2", list2); 

35         map.put("key3", list3);        

36         return map; 

37     } 

38     public ArrayList<Person> getList() { 

39         ArrayList <Person> list=new ArrayList<Person>(); 

40         Person p1=new Person(); 

41         p1.setName("name1"); 

42         p1.setPass("pass1"); 

43         Person p2=new Person(); 

44         p2.setName("name2"); 

45         p2.setPass("pass2"); 

46         Person p3=new Person(); 

47         p3.setName("name3"); 

48         p3.setPass("pass3"); 

49         list.add(p1); 

50         list.add(p2); 

51         list.add(p3); 

52         return list; 

53     } 

54     public Set<Person> getSet() { 

55         HashSet<Person>  set=new HashSet<Person>(); 

56         Person p1=new Person(); 

57         p1.setName("name1"); 

58         p1.setPass("pass1"); 

59         Person p2=new Person(); 

60         p2.setName("name2"); 

61         p2.setPass("pass2"); 

62         Person p3=new Person(); 

63         p3.setName("name3"); 

64         p3.setPass("pass3"); 

65         set.add(p1); 

66         set.add(p2); 

67         set.add(p3); 

68         return set; 

69     }    

70 } 

71 

72 1.2、com.yeeach.Person

73 package com.yeeach; 

74 

75 public class Person { 

76 

77         public String getName() { 

78             return name; 

79         } 

80         public void setName(String name) { 

81             this.name = name; 

82         } 

83         public String getPass() { 

84             return pass; 

85         } 

86         public void setPass(String pass) { 

87             this.pass = pass; 

88         } 

89         private String name; 

90         private String pass; 

91 

92 }

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" 

                   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 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++) 

                { 

                    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; 

    } 

}

  

 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

你可能感兴趣的:(Collection)