Flex与服务器交互
Flex与后端服务器的交互,主要有3种方式: LCDS、Http Service、Web Service 1.?LifeCycle Data Services,这个是Adobe的组件,是以一个war包的形式存在,所以它只能跟JAVA整合,并且它是需要钱买的,大概10W。它是通过调用远程对象的方式来访问我们的后端服务的,所以一般情况下,如果使用LCDS,就直接访问我们的Service业务层,而不需要通过Struts层,它使用Flex自带的协议,比HTTP协议要快。 ? <mx:RemoteObject id=""> <mx:method name="addUser"> <mx:arguments> <username></username> </mx:arguments> </mx:method> </mx:RemoteObject> 2.???? Http Service,即通过HTTP协议访问后端服务,像普通jsp页面访问后端服务一样,先访问Struts,再由Struts来调用我们的Service层。调用代码如下: (1)引入Http Service组件: <mx:HTTPService id="myAdd" url="http://localhost:8088/MyFlex/addUser.action" method="POST" result="myResponse(event)"> <!--请求的参数:如:username=zhaoql&password=123456 --> <mx:request> <username>{this.username.text}</username> <password>{this.password.text}</password> </mx:request> </mx:HTTPService>
private function doAddUser():void { this.myHttpService.send(); }
private function myResponse(event:ResultEvent):void { // result就是服务器端返回的结果 Alert(event.result); } ???? 注意: ???????? a> 默认情况,Flex中ResultEvent的result类型不支持JSON格式的数据, ???????? b> 我们可以下载第三方的包:as3corelib,然后将as3corelib.awc放到flex_libs目录, ???????? c> 然后使用JSON.decode(event.result);就可以返回一个JSON对象, ??????????????? 错误:{username:’zhaoql’,password:523} ?????????????? 正确:{“username”:”zhaoql”,“password”:523} 3.?Web Service (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |