zoukankan      html  css  js  c++  java
  • Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (三)

    五、复杂数据类型的通讯(http://xingfustar.cnblogs.com/)
        
        Remoting支持传送数组、List、HashTable、Dictionary等多种复杂数据类型,本文以数组,Dictionary,HashTable为例,讲解复杂数据类型的通讯。

    (一)数组(http://xingfustar.cnblogs.com/)

    1、.NET服务器端程序(http://xingfustar.cnblogs.com/)

        假设我们要做段程序,来获取所有的用户姓名。修改上节中的代码,增加一个GetUsers方法。代码如下:

            public string[] GetArray()
            {
                
    string[] array = new string[]{"张三","李四","王五"};

                
    return array;
            }


    2、Flex客户端程序(http://xingfustar.cnblogs.com/)

        在 Design 模式下添加,添加一个 Text文本控件,id为txtUsers,txt属性为空,添加一个 Button控件,id为btnGetArray,Label属性为 GetArray
        
        在 Source 模式下, 修改 mx:RemoteObject 标签,添加 
        <mx:method name="GetArray" result="RemoteResult(event)" fault="RemoteFault(event)"/> 
        修改脚本中 RemoteResult 方法,增加一个Case条件,代码如下

                public function RemoteResult(re:ResultEvent):void
                { 
                    
    switch(re.currentTarget.name)
                    {
                        
    case "HelloWord":
                            var str:String 
    = re.result as String;
                            
    this.txtHelloWord.text = str;
                            
    break;
                        
    case "SayHello":
                            str 
    = re.result as String;
                            
    this.txtSayHello.text = str;
                            
    break;
                        
    case "GetArray":
                            
    for(var i:int=0; i<re.result.length; i++)
                            {
                                
    this.txtUsers.text += re.result[i].toString() + "";
                            }
                            
    break;
                    }
                }

        在 mx:Button (GetArray 标签中添加属性 click="sampleRemoteObject.GetArray()"

        运行Flex程序,在浏览器中查看效果 

    (二)Dictionary(http://xingfustar.cnblogs.com/)

    1、.NET服务器端程序(http://xingfustar.cnblogs.com/)

        接下来我们做个Dictionary的例子,假设上例中,我们不光要获取用户姓名,而且我们还要得到他对应的年龄,我们使用Dictionary。修改代码,添加如下方法:

            public Dictionary<String, Int32> GetDictionary()
            {
                Dictionary
    <String, Int32> age = new Dictionary<stringint>();
                age.Add(
    "张三"23);
                age.Add(
    "李四"24);
                age.Add(
    "王五"22);
                
    return age;
            }


    2、Flex客户端程序(http://xingfustar.cnblogs.com/)

        在 Design 模式下添加,添加六个 Text文本控件,属性分别为 id = txtZhangSan,txt = ""; id = txtLiSi,txt = ""; id = txtWangWu,txt = ""; txt = "张三:"; txt = "李四:"; txt = "王五:"
        添加一个 Button控件,id为btnGetDictionary,Label属性为 GetDictionary
        
        在 Source 模式下, 修改 mx:RemoteObject 标签,添加 
        <mx:method name="GetDictionary" result="RemoteResult(event)" fault="RemoteFault(event)"/> 
        修改脚本中 RemoteResult 方法,增加一个Case条件,代码如下

                public function RemoteResult(re:ResultEvent):void
                { 
                    
    switch(re.currentTarget.name)
                    {
                        
    case "HelloWord":
                            var str:String 
    = re.result as String;
                            
    this.txtHelloWord.text = str;
                            
    break;
                        
    case "SayHello":
                            str 
    = re.result as String;
                            
    this.txtSayHello.text = str;
                            
    break;
                        
    case "GetUsers":
                            
    for(var i:int=0; i<re.result.length; i++)
                            {
                                
    this.txtUsers.text += re.result[i].toString() + "";
                            }
                            
    break;
                        
    case "GetDictionary":
                            
    this.txtZhangSan.text = re.result["张三"];
                            
    this.txtLiSi.text = re.result["李四"];
                            
    this.txtWangWu.text = re.result["王五"];
                            
    break;
                    }
                }

        在 mx:Button (GetDictionary) 标签中添加属性 click="sampleRemoteObject.GetDictionary()"

        运行Flex程序,在浏览器中查看效果 


    (三)HashTable(http://xingfustar.cnblogs.com/)

    1、.NET服务器端程序(http://xingfustar.cnblogs.com/)

        我们将上例中的Dictionary类型改成HashTable类型,代码如下:

            public Hashtable GetHashTable()
            {
                Hashtable hash 
    = new Hashtable();
                hash.Add(
    "张三"23);
                hash.Add(
    "李四"24);
                hash.Add(
    "王五"22);
                
    return hash;
            }


    2、Flex客户端程序(http://xingfustar.cnblogs.com/)

        借用下上例内容,在 Design 模式下添加一个 Button控件,id为btnGetHashTable,Label属性为 GetHashTable
        
        在 Source 模式下, 修改 mx:RemoteObject 标签,添加 
        <mx:method name="GetHashTable" result="RemoteResult(event)" fault="RemoteFault(event)"/> 
        修改脚本中 RemoteResult 方法,只需在上例  case "GetDictionary": 下一行加上 case "GetHashTable": 即可。

                        case "GetDictionary":
                        
    case "GetHashTable":
                            
    this.txtZhangSan.text = re.result["张三"];
                            
    this.txtLiSi.text = re.result["李四"];
                            
    this.txtWangWu.text = re.result["王五"];
                            
    break;

        在 mx:Button (GetHashTable) 标签中添加属性 click="sampleRemoteObject.GetHashTable()"

        运行Flex程序,在浏览器中查看效果 

    附件:完整代码(http://xingfustar.cnblogs.com/)
    1、.NET端

    /*----------------------------------------------------------------
     * 版权:
    http://XingFuStar.cnblogs.com
     * 
     * 文件名: RemotingSample
     * 文件功能描述: .NET与Flex通讯DEMO
     * 
     * 作者:XingFuStar
     * 日期:2007年12月11日
     * 
     * 当前版本:V1.0.0
     * 
     * 修改日期:2007年12月14日
     * 修改内容:增加获取数组,Dictionary,HashTable等方法例程
     *---------------------------------------------------------------
    */

    using System;
    using com.TheSilentGroup.Fluorine;
    using System.Collections.Generic;
    using System.Collections;

    namespace RemotingSample
    {
        [RemotingService(
    "Fluorine sample service")]
        
    public class RemotingSample
        {
            
    public RemotingSample()
            {
                
    //请不要删除以下信息
                
    //版权:http://XingFuStar.cnblogs.com
            }

            
    public string HelloWord()
            {
                
    return "Hello Word!";
            }

            
    public string SayHello(string name)
            {
                
    return "Hello " + name + "!";
            }

            
    public string[] GetArray()
            {
                
    string[] array = new string[]{"张三","李四","王五"};

                
    return array;
            }

            
    public Dictionary<String, Int32> GetDictionary()
            {
                Dictionary
    <String, Int32> dictionary = new Dictionary<stringint>();
                dictionary.Add(
    "张三"23);
                dictionary.Add(
    "李四"24);
                dictionary.Add(
    "王五"22);
                
    return dictionary;
            }

            
    public Hashtable GetHashTable()
            {
                Hashtable hash 
    = new Hashtable();
                hash.Add(
    "张三"23);
                hash.Add(
    "李四"24);
                hash.Add(
    "王五"22);
                
    return hash;
            }
        }
    }

    2、Flex端

    <?xml version="1.0" encoding="utf-8"?>
    <!--
    * 版权:http://XingFuStar.cnblogs.com
    *
    * 作者:XingFuStar
    * 日期:2007年12月11日
    *
    * 修改日期:2007年12月14日
    * 修改内容:增加获取数组,Dictionary,HashTable等方法例程。
    -->
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

        
    <mx:Script>
            
    <![CDATA[
                import mx.rpc.events.FaultEvent;
                import mx.rpc.events.ResultEvent;
                import mx.controls.Alert;
            
                public function RemoteResult(re:ResultEvent):void
                { 
                    switch(re.currentTarget.name)
                    {
                        case "HelloWord":
                            var str:String = re.result as String;
                            this.txtHelloWord.text = str;
                            break;
                        case "SayHello":
                            str = re.result as String;
                            this.txtSayHello.text = str;
                            break;
                        case "GetArray":
                            for(var i:int=0; i<re.result.length; i++)
                            {
                                this.txtUsers.text += re.result[i].toString() + ", ";
                            }
                            break;
                        case "GetDictionary":
                        case "GetHashTable":
                            this.txtZhangSan.text = re.result["张三"];
                            this.txtLiSi.text = re.result["李四"];
                            this.txtWangWu.text = re.result["王五"];
                            break;
                    }
                }
            
                public function RemoteFault(re:FaultEvent):void
                {
                    Alert.show("Message:" + re.fault.faultString,"出错");
                }           
            
    ]]>
        
    </mx:Script>

        
    <!--这里Source 对应.NET类,前面是命名空间,后面是类名 source = 命名空间.类名-->
        
    <mx:RemoteObject 
            
    id="sampleRemoteObject"
            destination
    ="fluorine"
            source
    ="RemotingSample.RemotingSample"
            showBusyCursor
    ="true">
            
    <!--这里是.NET中的方法,name = 方法名 -->
            
    <mx:method name="HelloWord" result="RemoteResult(event)" fault="RemoteFault(event)"/>    
            
    <mx:method name="SayHello" result="RemoteResult(event)" fault="RemoteFault(event)"/>            
            
    <mx:method name="GetArray" result="RemoteResult(event)" fault="RemoteFault(event)"/>
            
    <mx:method name="GetDictionary" result="RemoteResult(event)" fault="RemoteFault(event)"/>    
            
    <mx:method name="GetHashTable" result="RemoteResult(event)" fault="RemoteFault(event)"/>        
        
    </mx:RemoteObject>

        
    <mx:Text x="38" y="25" id="txtHelloWord"/>
        
    <mx:Button x="38" y="51" label="HelloWord" id="btnHelloWord0" click="sampleRemoteObject.HelloWord()"/>
        
        
    <mx:Text x="38" y="105" id="txtSayHello"/>
        
    <mx:Label x="38" y="131" text="name:"/>
        
    <mx:TextInput x="88" y="129" id="txtName"/>
        
    <mx:Button x="256" y="129" label="SayHello" id="btnSayHello" click="sampleRemoteObject.SayHello(this.txtName.text)"/>
        
        
    <mx:Text x="38" y="181" id="txtUsers"/>
        
    <mx:Button x="38" y="207" label="GetArray" id="btnGetArray" click="sampleRemoteObject.GetArray()"/>
        
        
    <mx:Label x="38" y="262" text="张三:"/>
        
    <mx:Text x="76" y="262" id="txtZhangSan"/>
        
    <mx:Label x="129" y="262" text="李四:"/>
        
    <mx:Text x="167" y="262" id="txtLiSi"/>
        
    <mx:Label x="218" y="262" text="王五:"/>
        
    <mx:Text x="256" y="262" id="txtWangWu"/>
        
    <mx:Button x="38" y="288" label="GetDictionary" id="btnGetDictionary" click="sampleRemoteObject.GetDictionary()"/>
        
    <mx:Button x="157" y="288" label="GetHashTable" id="btnGetHashTable" click="sampleRemoteObject.GetHashTable()"/>
    </mx:Application>


    本节内容到此结束,其实本节的例子都差不多,相信讲到这里,看过文章的人对Remoting通讯有了大概的了解,只要熟悉AS3语言,以上这些都不困难,关于这个通讯,我计划还有一节,主要讲解自定义实体对象的传送。

  • 相关阅读:
    Spring创建复杂对象
    navicat中mysql中文数据显示乱码
    cmd黑窗口查询数据库中数据时中文乱码
    springboot中的文件上传与下载
    springboot中定义拦截器
    springboot中的AOP开发
    springboot集成logback日志
    开启springboot中对jsp页面修改后的热部署
    IntelliJ Idea中常用快捷键(持续更新中)
    jsp页面中格式化展示日期
  • 原文地址:https://www.cnblogs.com/tianlangshu/p/2472803.html
Copyright © 2011-2022 走看看