如何使用mx:RemoteObject

来源:互联网 发布:淘宝店铺怎么申请 编辑:程序博客网 时间:2024/05/18 19:37

第一步:创建flex项目时指明项目使用远程数据调用,并指明远程服务的contextroot和服务器位置

第二步:在Flex组件中定义RemoteObject

<mx:RemoteObject id="freshQuotes" destination="Portfolio" fault="onFault(event);">

//   id用于被flex中的其他方法调用的引用

//destination 与 remoting-config.xml中的目标配置一致

//fault指定远程调用错误的回调方法

  

<mx:method name="getQuotes"   concurrency="last"     mx:method指明远程对象的被调用方法

       result="onResult(event);"                         result指定结果返回的回调方法

   />

</mx:RemoteObject>

第三步:编写Java服务端对象,简单对象

第四步:配置remoting-config.xml

<destination id="Portfolio">                               id与RemoteObject标签中的destination一致

         <properties>

             <source>com.favzone.action.Portfolio</source>       给出服务端类路径

         </properties>

</destination>

第五步:部署web应用,该web应用要含有编译好的swf文件

===============================================================================

Java+Flex整合应用简单示例(mx:RemoteObject)

1.java 

Java代码 复制代码
  1. package com;   
  2.   
  3. public class LoginDemo {   
  4.        
  5.     public String validate(String username,String password){   
  6.         String message ="login failed!";   
  7.         if(username.equals("lin")&&password.equals("lin")){   
  8.             message = "login successed!";   
  9.         }   
  10.         return message;   
  11.     }   
  12.   
  13. }  



2.remoting-config.xml 

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <service id="remoting-service"    
  3.     class="flex.messaging.services.RemotingService">   
  4.   
  5.     <adapters>   
  6.         <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>   
  7.     </adapters>   
  8.   
  9.     <default-channels>   
  10.         <channel ref="my-amf"/>   
  11.     </default-channels>   
  12.        
  13.     <destination id="login">   
  14.         <properties>   
  15.             <source>com.LoginDemo</source>   
  16.         </properties>       
  17.     </destination>   
  18.        
  19. </service>  




3.mxml 

Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">   
  3.     <mx:Script>   
  4.         <![CDATA[   
  5.             import mx.rpc.events.FaultEvent;   
  6.             import mx.rpc.events.ResultEvent;   
  7.             import mx.controls.Alert;   
  8.                
  9.             [Bindable]   
  10.             var returnValue:String;   
  11.             var username1:String;   
  12.             var password1:String;   
  13.             function sendRequest():void{   
  14.                 username1=username.text;   
  15.                 password1=password.text;                   
  16.                 ro.validate(username1,password1);   
  17.                 ro.addEventListener(ResultEvent.RESULT,results);   
  18.                            
  19.             }   
  20.                
  21.             function results(event:ResultEvent):void{   
  22.                 returnValue=event.result as String;   
  23.                    
  24.             }   
  25.                
  26.             function faultHandler(event:FaultEvent):void{   
  27.                 Alert.show(event.fault.toString());   
  28.             }   
  29.         ]]>   
  30.     </mx:Script>   
  31.        
  32.     <mx:RemoteObject id="ro" destination="login" fault="faultHandler(event)">   
  33.     </mx:RemoteObject>   
  34.     <mx:Panel height="400" width="400" layout="absolute" title="用户登录">   
  35.         <mx:Label x="50" y="50" text="用户名" width="50"></mx:Label>   
  36.         <mx:Label x="50" y="75" text="密码" width="50"></mx:Label>   
  37.         <mx:TextInput id="username" x="75" y="50"/>   
  38.         <mx:TextInput id="password" x="75" y="75"/>   
  39.         <mx:Button x="50" y="100" label="登录" click="sendRequest()"/>   
  40.         <mx:Label x="50" y="130" text="{returnValue}"/>      
  41.     </mx:Panel>   
  42. </mx:Application>  

0 0