<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    The NoteBook of EricKong

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

    In my previous tutorial on BlazeDS, I went over an example over how to setup remote objects with data push. There are however, a lot of instances where someone just wanted to receive data and they aren’t concerned with it being updated., so it turns more into a ‘client requesting data whenever it wants it’ scenario. This article will not be nearly as indepth as the last blazeDS article, but if you have not setup your environment for BlazeDS before, it will probably be in your best interest to check it out.

    Lets begin by making sure that we have our server and BlazeDs war file. Both of them can be found here:

    Tomcat 6
    BlazeDS

    Once again, we are going to extract the contents into our BlazeDS server project. This time, we are only going to edit remoting-config.xml. Make sure that the following lines are in there.

    <default-channels>
       <channel ref="my-amf"/>
    </default-channels>
    <destination id="BlazeDsService"> 
      <properties> 
         <source>com.codeofdoom.BlazeDsService</source> 
      </properties> 
    </destination>

    We tell the remoting-config to use the channel “my-amf”, which is already defined with the services-config.xml file. Next we are telling it to use the the destination “BlazeDsService”. This is what our remote object will be looking at. The properties also contains a source. This source is going to be a java class with the methods it needs in order to process the calls from the front end. You must make sure you include the fully qualified name, so we will call it com.codeofdoom.BlazeDsService. So what does this file look like?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    package com.codeofdoom;
     
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import flex.messaging.io.ArrayCollection;
     
    public class BlazeDsService {
        private static final String[] MASTER_LIST = {"C", "FNM", "FRE", "F", "GOOG", "AIG", "CSCO", "MSFT", "AAPL", "YHOO", "BSX", "PORT","F", "TNT", "ESP", "RET", "VBN", "EES"};
        public BlazeDsService(){}
        public List<StockQuote> getQuotes(){
            List<StockQuote> list = new ArrayList<StockQuote>();
            Random r = new Random();
            for (String s:MASTER_LIST){
                StockQuote sq = new StockQuote();
                sq.setName(s);
                sq.setPrice(r.nextInt(50));
                list.add(sq);
            }
            return list;
        }
    }

    Yep. Thats it. It not implementing anything crazy, not extending any service adapters, anything like that. It’s just a java class with some functions on it. Next we are going to create the object that we will be sending back and forth. In this example, we are going to be simulating retrieving some stock data, so I called the class StockQuote.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    package com.codeofdoom;
     
    public class StockQuote {
        private String name;
        private int price;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getPrice() {
            return price;
        }
        public void setPrice(int price) {
            this.price = price;
        }
    }

    On the front end, we are going to need the matching object, and here that is.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    package com.codeofdoom.dto
    {
        [Bindable]
        [RemoteClass(alias="com.codeofdoom.StockQuote")]
        public class StockQuote{
            private var _name:String
            private var _price:int;
     
            public function get name():String{
                return _name;
            }
            public function get price():int{
                return _price;
            }
            public function get name(name:String):void{
                _name = name;
            }
            public function get price(price:int):void{
                _price = price;
            }
        }
    }

    I mentioned this in the last article, but I am going to mention this again, just to stress the importance. When we are using Remote Object, we must make sure that

    • All the properties are the same.
    • We have getters and setters for all properties on back the java side and the AS side.
    • We must have our AS object set as Bindable and the RemoteClass set to the path of our remote object.
    • We must make sure that the constructors match. I have seen where flex will swallow the error message if they don’t match, making debugging a pain.

    Now lets take a look at the mxml.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:Script>
            <![CDATA[
                import mx.rpc.events.ResultEvent;
                import mx.collections.ArrayCollection;
                import mx.messaging.messages.IMessage;
                import mx.messaging.events.MessageAckEvent;
                import mx.messaging.messages.AsyncMessage;
                import mx.messaging.events.MessageEvent;
     
                public function retrieveStocks():void{
                    ro.getQuotes();
                }
                private function result(e:ResultEvent):void{
                    stockChart.dataProvider = e.result as ArrayCollection;
                }
            ]]>
        </mx:Script>
        <mx:RemoteObject id="ro" result="result(event)" destination="BlazeDsService"/>
        <mx:VBox width="100%">
            <mx:BarChart width="100%" id="stockChart">
                <mx:horizontalAxis>
                    <mx:CategoryAxis categoryField="name"/>
                </mx:horizontalAxis>
                <mx:series>
                    <mx:ColumnSeries xField="name" yField="price"/>
                </mx:series>
            </mx:BarChart>
            <mx:Button label="Retrieve Stocks" click="retrieveStocks()"/>
        </mx:VBox>   
    </mx:Application>

    Once again, not much to it. There are three areas of importance.

    • Our remote object is that is subscribed to our destination that we declared within our remoting-config.xml.
    • The retrieveQuotes function is what calls our backend. Note that it just calls ‘getQuotes’ directly from the remote object.
    • The result function that is set on the RemoteObject is what comes back from the back end. Now a lot of examples will do something to the grid such as dataprovider = {ro.getQuotes.lastResult}. We could do the same here, but i wanted to give you a chance to inspect the object to see what exactly is coming back.

    Once you click the Retrieve Stocks button, it then calls the getQuotes() function on the remote object. That then builds our List of objects and passes them to the front end. When it comes back, we then just apply it to the dataprovider of our chart.

    I really wanted to take the time out to show you some other ways of handling your data within BlazeDS. It is an extremely powerful tool that every Flex developer should have under their belt. Its a lot less cumbersome than standard calls to the back end and the amount of data that is sent back and forth is a lot smaller than sending plane XML over the wire. Good luck!

    Here is the source

    posted on 2011-07-02 16:59 Eric_jiang 閱讀(494) 評論(0)  編輯  收藏 所屬分類: Flex
    主站蜘蛛池模板: 亚洲AV无码AV男人的天堂| 亚洲成AV人片在线观看无码| 国产精品国产午夜免费福利看| 亚洲精品高清视频| 日韩精品免费一级视频| 亚洲AV无码专区在线电影成人| 亚洲性在线看高清h片| 久久精品视频免费看| 亚洲国产欧洲综合997久久| 亚洲国产精品人人做人人爽| 亚在线观看免费视频入口| 亚洲日韩国产二区无码| 亚洲色欲久久久综合网东京热| 人禽伦免费交视频播放| 小小影视日本动漫观看免费| 久久毛片免费看一区二区三区| 亚洲福利视频网址| 亚洲国产精品人人做人人爱| 亚洲人成免费电影| 一级片在线免费看| 亚洲一区在线免费观看| 黑人大战亚洲人精品一区| 无码人妻精品一二三区免费| 中文在线观看永久免费| 亚洲人成人无码.www石榴| 亚洲国产精品久久66| 免费一级特黄特色大片在线观看| 免费A级毛片无码A∨免费| jizz免费在线影视观看网站| 亚洲私人无码综合久久网| 亚洲AV美女一区二区三区| 亚洲国产精品狼友中文久久久| 一二三四影视在线看片免费| 日本高清高色视频免费| 国产亚洲精品国产福利在线观看| 亚洲剧场午夜在线观看| 亚洲成AV人片一区二区密柚| 亚洲v国产v天堂a无码久久| 巨胸喷奶水视频www网免费| 精品久久久久久亚洲综合网| 亚洲剧场午夜在线观看|