zoukankan      html  css  js  c++  java
  • Flex+Struts2+JSON实现Flex和后台的HTTP Service请求

    http://www.fengfly.com/plus/view-191093-1.html

    Flex+Struts2+JSON的后台代码我在这就不多说了。不懂得请看我写的上一篇文章《Struts2+JQuery+JSON实现异步交互》那篇文章,后台没有任何变化。

    在这着重讲Flex端的实现代码。

    第一步:

         从http://code.google.com/p/as3corelib/网站中下载as3corelib-.92.1.zip文件并解压,解压后在as3corelib-.92.1as3corelib-.92.1lib目录中有一个as3corelib.swc文件,把该文件复制到你的Flex工程的libs目录中。就可以工作了。请看Flex代码:

    Flex代码代码
    1. <?xml version="1.0" encoding="utf-8"?>   
    2. <mx:Application xmlns:mx="<A href="http://www.adobe.com/2006/mxml">http://www.adobe.com/2006/mxml</A>" fontSize="12" layout="absolute">   
    3.  <mx:Script>   
    4.   <![CDATA[   
    5.    import com.adobe.serialization.json.JSONDecoder;   
    6.    import mx.rpc.events.FaultEvent;   
    7.    import mx.controls.Alert;   
    8.    import mx.rpc.events.ResultEvent;   
    9.       
    10.    internal function sendURL():void{   
    11.     //设置HTTPService的url属性为你要访问的Action连接后面dd=new Date().getTime();是为了消除浏览器缓存   
    12.     hs.url = "<A href="http://localhost:8080/fsj/flexstrutsjson!hdList.action?dd=%22+new">http://localhost:8080/fsj/flexstrutsjson!hdList.action?dd="+new</A> Date().getTime();   
    13.     //调用HTTPService的send()方法   
    14.     hs.send();   
    15.     //注册成功事件   
    16.     hs.addEventListener(ResultEvent.RESULT,success);   
    17.     //注册失败事件   
    18.     hs.addEventListener(FaultEvent.FAULT,faultResult);   
    19.    }   
    20.    //失败事件调用的函数   
    21.    internal function faultResult(event:FaultEvent):void{   
    22.     //弹出失败信息   
    23.     Alert.show(event.fault.message);   
    24.    }   
    25.    //成功函数   
    26.    internal function success(event:ResultEvent):void{   
    27.     //把返回的对象转换成字符串   
    28.     var userStr:String = event.result.toString();   
    29.     //使用刚才加入的swc包包中的类JSONDecoder()把字符串转换成JSONDecoder对象   
    30.     var userJson:JSONDecoder = new JSONDecoder(userStr);   
    31.     //返回Map的方式   
    32. //    var d:Object = userJson.getValue().uerInfoMap;   
    33. //    var arryObject:Array=[];   
    34. //    for each(var f:Object in d){   
    35. //     arryObject.push(f);   
    36. //    }   
    37. //    userInfo.dataProvider = arryObject;   
    38.     //返回List的处理方式   
    39.     //使用JSONDecoder对象的getValue方法返回对象   
    40.     userInfo.dataProvider = userJson.getValue().userInfos;   
    41.    }   
    42.   ]]>   
    43.  </mx:Script>   
    44.  <!--创建HTTPService对象-->   
    45.  <mx:HTTPService id="hs" method="POST" showBusyCursor="true"/>   
    46.  <!--创建发送按钮并调用sendURL()函数-->   
    47.  <mx:Button id="requstDate" click="sendURL()" label="发送"  x="95" y="78"/>   
    48.  <!--创建DataGrid控件来绑定返回的数据-->   
    49.  <mx:DataGrid x="95" y="131" width="482" height="270" id="userInfo">   
    50.   <mx:columns>   
    51.    <mx:DataGridColumn headerText="用户名" dataField="userName"/>   
    52.    <mx:DataGridColumn headerText="密码" dataField="pwd"/>   
    53.    <mx:DataGridColumn headerText="Email" dataField="email"/>   
    54.    <mx:DataGridColumn headerText="年龄" dataField="age"/>   
    55.   </mx:columns>   
    56.  </mx:DataGrid>   
    57. </mx:Application>  

    需要注意的是:在JSON的数据量比较大的情况下该方法执行的速度会比较慢。

    相关文章:http://www.infoq.com/cn/articles/flex-xml-json

  • 相关阅读:
    S5pv210 android VGA 1440*900 视频播放/3D 演示效果实拍视频
    毕业3年,工资从5k到20k的经历——真的还是假的啊?
    转载.WinCE6.0 Camera驱动整体结构
    半夜来认识一下S5PV210 的VBPDE和VFPDE
    微软的windows 8授权真的要这么贵?谁要?!
    基于S5PC100的FIMC的部分解释——一篇让我理解透彻2440和S5PV210 摄像头camera控制器的文章
    转.Buffer Management by the Camera Driver (Windows Embedded CE 6.0)
    微软?想干掉苹果,自己做平板,还想做手机,有优秀的合作伙伴诺基亚?简直是笑话,难怪鲍ceo 被评为最应该下台的CEO了
    魅族经过M8——》M9——》MX 已经走向国际?
    wince6.0 S5pv210 之Sate210 VGA 镜像1440*900@60HZ/1280*1024@70HZ分辨率测试镜像(南嵌电子科技作品)
  • 原文地址:https://www.cnblogs.com/yaowen/p/4208913.html
Copyright © 2011-2022 走看看