Flex学习进级-Flex Date Server 2 用之初体验

来源:互联网 发布:adobepc软件下载 编辑:程序博客网 时间:2024/06/05 19:20
学 习了前面对FLEX应用有所了解,但是就像Flash Media Server一样,为了配合Flex与后台的通讯,ADOBE推出了专用于Flex的后台服务即Flex Date Server 2,今天初试了一下,使用起来真的很不错,很方便,但是由于知识并不是很全面,对AS3类,还有很多没有了解所以只能简单的实现它的效果,把我的制作步骤 拿出来和大家分享,毕竟做出来更有信心向后面学习了,不是吗??!!好了,废话就不多少了。

这次的前期工作比较繁杂,因为要安装FLEX DATE SERVER才可以实现效果,好再ADOBE比较大方暂时不需要输入序列号也可以使用,当然功能一定是受到限制的,但是作为学习来用已经绰绰有余了。

下载地址:Download the free Flex Data Services 2 Express edition

可以下载到本机,然后安装,安装的具体步骤可以从网上搜索一下,有很多包括和IIS,TOMCAT整合的文章。这里面就不说了。

开启服务方法:开始>Adobe > Flex Data Services 2 > Start Integrated Flex Server ,好了,开启了(Ctrl + c是停止).


下面首先建立一个新的项目。建议使用FLEX BUILDING2作为开发工具,因为使用SDK会有很多不方便的地方。

在新建项目的时候可以选择类型,这次选择Flex Data Services,其他默认即可,为这个项目取个名字吧。
下面要在服务器上建立一个ID,使FLEX通过ID进行通讯。
要修改的文件路径为*:/fds2/jrun4/servers/default/flex/WEB-INF/flex 中的 data-management-config.xml
该后的文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<service id="data-service"
class="flex.data.DataService"
messageTypes="flex.data.messages.DataMessage">

<adapters>
<adapter-definition id="actionscript" class="flex.data.adapters.ASObjectAdapter" default="true"/>
<adapter-definition id="java-dao" class="flex.data.adapters.JavaAdapter"/>
</adapters>

<destination id="notes">
<adapter ref="actionscript"/>
<properties>
<metadata>
<identity property="noteId"/>
</metadata>
</properties>
</destination>

</service>

下面就开始创建用户UI了,话不多说了节省点篇幅,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" >
<mx:TextArea id="log" width="500" height="300" horizontalCenter="0" y="33"/>
<mx:Button label="Send" y="345.1" />
</mx:Application>

然后添加调用脚本
    <mx:Script>
        <![CDATA[
            import mx.data.DataService;
            import mx.data.events.*;
            import mx.rpc.AsyncToken;
            import mx.rpc.events.*;
            import mx.messaging.events.*;
            import mx.utils.ObjectProxy;
           
            public var noteObj:Object = new Object();
            public var getToken:AsyncToken;
            private var ds:DataService;
            [Bindable]
            public var noteProxy:ObjectProxy;
           
            public function initApp():void {
                ds = new DataService("notes");
                ds.addEventListener(ResultEvent.RESULT, resultHandler);
                ds.autoCommit = false;
                noteObj.noteId = 1;
                noteObj.noteText = "Type your notes here and share them with other clients!";
                getToken = ds.getItem(noteObj, noteObj);
            }
           
            public function resultHandler(event:ResultEvent):void
           {
              if (event.token == getToken)
                  noteProxy = ObjectProxy(event.result);
           }
        ]]>
    </mx:Script>
接着就是添加动作事件:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" creationComplete="initApp();">
    <mx:Script>
        <![CDATA[
            import mx.data.DataService;
            import mx.data.events.*;
            import mx.rpc.AsyncToken;
            import mx.rpc.events.*;
            import mx.messaging.events.*;
            import mx.utils.ObjectProxy;
           
            public var noteObj:Object = new Object();
            public var getToken:AsyncToken;
            private var ds:DataService;
            [Bindable]
            public var noteProxy:ObjectProxy;
           
            public function initApp():void {
                ds = new DataService("notes");
                ds.addEventListener(ResultEvent.RESULT, resultHandler);
                ds.autoCommit = false;
                noteObj.noteId = 1;
                noteObj.noteText = "Type your notes here and share them with other clients!";
                getToken = ds.getItem(noteObj, noteObj);
            }
           
            public function resultHandler(event:ResultEvent):void
           {
              if (event.token == getToken)
                  noteProxy = ObjectProxy(event.result);
           }
        ]]>
    </mx:Script>
    <mx:Binding source="log.text" destination="noteProxy.noteText"/>
    <mx:TextArea id="log" width="500" height="300" text="{noteProxy.noteText}" horizontalCenter="0" y="33"/>
    <mx:Button label="Send" y="345.1" click="ds.commit();" horizontalCenter="0"/>   
</mx:Application>
然后可以发布了,实验的时候最好是开两个窗口,当一个输入后点击SEND按钮,在另一端就会看到消息被添加了。快试试吧。