您的位置:首页 > 移动开发 > Objective-C

如何使用mx:RemoteObject

2014-03-06 10:27 435 查看
第一步:创建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代码 


package com;   
  
public class LoginDemo {   
       
    public String validate(String username,String password){   
        String message ="login failed!";   
        if(username.equals("lin")&&password.equals("lin")){   
            message = "login successed!";   
        }   
        return message;   
    }   
  
}  

2.remoting-config.xml 

Java代码 


<?xml version="1.0" encoding="UTF-8"?>   
<service id="remoting-service"    
    class="flex.messaging.services.RemotingService">   
  
    <adapters>   
        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
  
    </adapters>   
  
    <default-channels>   
        <channel ref="my-amf"/>   
    </default-channels>   
       
    <destination id="login">   
        <properties>   
            <source>com.LoginDemo</source>   
        </properties>       
    </destination>   
       
</service>  

3.mxml 

Java代码 


<?xml version="1.0" encoding="utf-8"?>   
<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;   
               
            [Bindable]   
            var returnValue:String;   
            var username1:String;   
            var password1:String;   
            function sendRequest():void{   
                username1=username.text;   
                password1=password.text;                   
                ro.validate(username1,password1);   
                ro.addEventListener(ResultEvent.RESULT,results);   
                           
            }   
               
            function results(event:ResultEvent):void{   
                returnValue=event.result as String;   
                   
            }   
               
            function faultHandler(event:FaultEvent):void{   
                Alert.show(event.fault.toString());   
            }   
        ]]>   
    </mx:Script>   
       
    <mx:RemoteObject id="ro" destination="login" fault="faultHandler(event)">   
    </mx:RemoteObject>   
    <mx:Panel height="400" width="400" layout="absolute" title="用户登录">   
        <mx:Label x="50" y="50" text="用户名" width="50"></mx:Label>   
        <mx:Label x="50" y="75" text="密码" width="50"></mx:Label>   
        <mx:TextInput id="username" x="75" y="50"/>   
        <mx:TextInput id="password" x="75" y="75"/>   
        <mx:Button x="50" y="100" label="登录" click="sendRequest()"/>   
        <mx:Label x="50" y="130" text="{returnValue}"/>      
    </mx:Panel>   
</mx:Application>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: