Flex+Struts2+JSON 实现异部提交

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

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

第一步:

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

Flex代码代码
<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="<A href="http://www.adobe.com/2006/mxml">http://www.adobe.com/2006/mxml</A>" fontSize="12" layout="absolute">  
<mx:Script>  
  <![CDATA[  
   import com.adobe.serialization.json.JSONDecoder;  
   import mx.rpc.events.FaultEvent;  
   import mx.controls.Alert;  
   import mx.rpc.events.ResultEvent;  
     
   internal function sendURL():void{  
    //设置HTTPService的url属性为你要访问的Action连接后面dd=new Date().getTime();是为了消除浏览器缓存  
    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();  
    //调用HTTPService的send()方法  
    hs.send();  
    //注册成功事件  
    hs.addEventListener(ResultEvent.RESULT,success);  
    //注册失败事件  
    hs.addEventListener(FaultEvent.FAULT,faultResult);  
   }  
   //失败事件调用的函数  
   internal function faultResult(event:FaultEvent):void{  
    //弹出失败信息  
    Alert.show(event.fault.message);  
   }  
   //成功函数  
   internal function success(event:ResultEvent):void{  
    //把返回的对象转换成字符串  
    var userStr:String = event.result.toString();  
    //使用刚才加入的swc包包中的类JSONDecoder()把字符串转换成JSONDecoder对象  
    var userJson:JSONDecoder = new JSONDecoder(userStr);  
    //返回Map的方式  
//    var d:Object = userJson.getValue().uerInfoMap;  
//    var arryObject:Array=[];  
//    for each(var f:Object in d){  
//     arryObject.push(f);  
//    }  
//    userInfo.dataProvider = arryObject;  
    //返回List的处理方式  
    //使用JSONDecoder对象的getValue方法返回对象  
    userInfo.dataProvider = userJson.getValue().userInfos;  
   }  
  ]]>  
</mx:Script>  
<!--创建HTTPService对象-->  
<mx:HTTPService id="hs" method="POST" showBusyCursor="true"/>  
<!--创建发送按钮并调用sendURL()函数-->  
<mx:Button id="requstDate" click="sendURL()" label="发送"  x="95" y="78"/>  
<!--创建DataGrid控件来绑定返回的数据-->  
<mx:DataGrid x="95" y="131" width="482" height="270" id="userInfo">  
  <mx:columns>  
   <mx:DataGridColumn headerText="用户名" dataField="userName"/>  
   <mx:DataGridColumn headerText="密码" dataField="pwd"/>  
   <mx:DataGridColumn headerText="Email" dataField="email"/>  
   <mx:DataGridColumn headerText="年龄" dataField="age"/>  
  </mx:columns>  
</mx:DataGrid>  
</mx:Application> 

你可能感兴趣的:(jquery,json,Flex,Google,Adobe)