Flex 与 Java通信 RemoteObject 方式

原文出自:http://blog.csdn.net/youqishini/article/details/7462002



在学习了flash中的事件机制后,我们就开始学习flex与Java中的3种通信方式。Flex与Java通信有3种方式:

 

      ●flex访问Java普通类使用RemoteObject方式,这也是用的最多的一种方式。


      ●flex访问Java服务器类(如servlet)用HttpService方式。


      ●Flex与WebService交互用WebService方式。

 

       今天就先来学习flex访问普通Java类。在学习之前我们需要考虑这么一个问题:由于我们这3个例子都是以登陆为例子进行的,所以尽量让登陆界面分离出来可以重复使用,这用到Flex中的module。我们怎么将module中的数值传到父窗口去呢?我们又想到上节中学习的自定义事件。好了,既然想通了,就开始今天的学习。

 

        将MyEclipse切换到MyEclipse视图,新建一个与flex交互的普通Java类,代码如下所示:


[java]  view plain copy print ?
  1. package com.yqsn.test;  
  2.    
  3. public class RemoteObjectDemo {  
  4.     public boolean login(String username,String passworld ){  
  5.         
  6.        if(username.equals("admin")&&passworld.equals("123")){  
  7.            return true;  
  8.        }else{  
  9.            return false;  
  10.        }  
  11.         
  12.     }  
  13. }  


 

       在WebRoot/WEB-INF/flex目录下的remoting-config.xml文件中添加如下所示代码:


[html]  view plain copy print ?
  1. <destination id="remoteObjectDemo">  
  2.        <properties>  
  3.            <source>com.yqsn.test.RemoteObjectDemo</source>  
  4.        </properties>  
  5.     </destination>  


 

 

       将MyEclipse切换到Flash视图,首先自定义一个事件类LoginEvent.as,为以后传值服务,代码如下所示:


[html]  view plain copy print ?
  1. package com.flex.ases  
  2. {  
  3.     import flash.events.Event;  
  4.      
  5.     public class LoginEvent extends Event  
  6.     {    
  7.        public static const LOGIN_EVENT:String="LOGIN_EVENT";  
  8.         
  9.        private  var _loginMess:Object;  
  10.         
  11.        public function LoginEvent(type:String,loginMess:Object=nullbubbles:Boolean=false,cancelable:Boolean=false)  
  12.        {  
  13.            this._loginMess=loginMess;  
  14.            super(type, bubbles, cancelable);  
  15.        }  
  16.         
  17.        public function get loginMess():Object  
  18.        {  
  19.            return _loginMess;  
  20.        }  
  21.    
  22.        public function set loginMess(value:Object):void  
  23.        {  
  24.            _loginMess = value;  
  25.        }  
  26.    
  27.     }  
  28. }  




     在这个类中我们定义了一个事件类型LOGIN_EVENT,定义了一个Object类型的变量,用于存值。


     接着新建一个登陆信息的VO类LoginMess.as,为以后存贮用户信息服务,代码如下所示:


[html]  view plain copy print ?
  1. package com.flex.ases  
  2. {  
  3.     public class LoginMess  
  4.     {  
  5.         
  6.        private  var _username:String;  
  7.         
  8.        private var _passworld:String;  
  9.         
  10.        public function LoginMess()  
  11.        {  
  12.             
  13.        }  
  14.    
  15.        public function get passworld():String  
  16.        {  
  17.            return _passworld;  
  18.        }  
  19.    
  20.        public function set passworld(value:String):void  
  21.        {  
  22.            _passworld = value;  
  23.        }  
  24.    
  25.        public function get username():String  
  26.        {  
  27.            return _username;  
  28.        }  
  29.    
  30.        public function set username(value:String):void  
  31.        {  
  32.            _username = value;  
  33.        }  
  34.    
  35.     }  
  36. }  


 

        新建一个用于登陆的MXMLModule文件LoginModule.mxml,代码如下所示:

 

[html]  view plain copy print ?
  1. <?xmlversionxmlversion="1.0" encoding="utf-8"?>  
  2. <s:Module xmlns:fx="http://ns.adobe.com/mxml/2009"  
  3.          xmlns:s="library://ns.adobe.com/flex/spark"  
  4.          xmlns:mx="library://ns.adobe.com/flex/mx" width="256" height="213">  
  5.      
  6.     <fx:Script>  
  7.        <![CDATA[ 
  8.            import com.flex.ases.LoginEvent; 
  9.            
  10.            import mx.controls.Alert; 
  11.            import mx.events.CloseEvent; 
  12.            import mx.managers.PopUpManager; 
  13.            
  14.            protected function login_clickHandler(event:MouseEvent):void 
  15.            { 
  16.               // TODOAuto-generated method stub 
  17.               var loginMess:Object=new Object; 
  18.               loginMess.username=userName.text; 
  19.               loginMess.passworld=passworld.text; 
  20.               if(userName.text=="" ||passworld.text==""){ 
  21.                   Alert.show("用户名或密码不能为空!"); 
  22.                   return; 
  23.               } 
  24.               this.dispatchEvent(newLoginEvent(LoginEvent.LOGIN_EVENT,loginMess)); 
  25.               userName.text=""; 
  26.               passworld.text=""; 
  27.               PopUpManager.removePopUp(this); 
  28.            } 
  29.            
  30.            protected function loginTitleWindow_closeHandler(event:CloseEvent):void 
  31.            { 
  32.               // TODO Auto-generatedmethod stub 
  33.               userName.text=""; 
  34.               passworld.text=""; 
  35.               PopUpManager.removePopUp(this); 
  36.            } 
  37.            
  38.        ]]>  
  39.     </fx:Script>  
  40.      
  41.     <fx:Declarations>  
  42.        <!-- Place non-visualelements (e.g., services, value objects) here -->  
  43.     </fx:Declarations>  
  44.         
  45.         <s:TitleWindow x="1" y="1" width="256"height="213" title="登陆"id="loginTitleWindow" close="loginTitleWindow_closeHandler(event)" >  
  46.    
  47.        <s:Form width="100%" height="183" >  
  48.             
  49.            <s:FormItem left="60" height="39" width="224" label="用户名" required="true"  >  
  50.               <s:TextInput id="userName" />  
  51.            </s:FormItem>  
  52.             
  53.            <s:FormItem required="true" width="224" label="密码" >  
  54.               <s:TextInput id="passworld" displayAsPassword="true" />  
  55.            </s:FormItem>      
  56.             
  57.            <s:FormItem width="227">  
  58.               <s:Button id="login" label="登陆" click="login_clickHandler(event)"/>  
  59.            </s:FormItem>  
  60.         
  61.        </s:Form>  
  62.             
  63.        </s:TitleWindow>   
  64. </s:Module>  


 

        这个页面以后我们反复使用,这就是module文件的优点之一。在这个页面中我们不处理与Java交互的部分,因为既然是公共页面,我们应该将于Java交互的部分放在相应引用的文件中。


         接着创建主页面RemoteObjectDemo.mxml,代码如下所示:

 

[html]  view plain copy print ?
  1. <?xml version="1.0"encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"  
  3.               xmlns:s="library://ns.adobe.com/flex/spark"  
  4.               xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%">  
  5.      
  6.     <fx:Script>  
  7.        <![CDATA[ 
  8.            import com.flex.ases.LoginEvent; 
  9.            import com.flex.ases.LoginMess; 
  10.            import com.flex.component.LoginTitleWindow; 
  11.            import com.flex.module.LoginModule; 
  12.            
  13.            import mx.collections.ArrayCollection; 
  14.            import mx.controls.Alert; 
  15.            import mx.managers.PopUpManager; 
  16.            import mx.rpc.events.FaultEvent; 
  17.            import mx.rpc.events.ResultEvent; 
  18.            [Bindable] 
  19.            private var loginMess:LoginMess=new LoginMess(); 
  20.            private var loginModule:LoginModule=new LoginModule(); 
  21.            protected function login_clickHandler(event:MouseEvent):void 
  22.            { 
  23.               PopUpManager.addPopUp(loginModule,this,true); 
  24.               PopUpManager.centerPopUp(loginModule); 
  25.               loginModule.addEventListener(LoginEvent.LOGIN_EVENT,getLoginMess); 
  26.            } 
  27.            public function getLoginMess(event:LoginEvent):void{ 
  28.               var username:String=event.loginMess['username']; 
  29.               var passworld:String=event.loginMess['passworld']; 
  30.               loginMess.username=username; 
  31.               remoteObj.login(username,passworld); 
  32.               
  33.            } 
  34.            
  35.            protected function remoteObj_resultHandler(event:ResultEvent):void 
  36.            { 
  37.               // TODOAuto-generated method stub 
  38.               var str:Boolean=event.result as Boolean; 
  39.               if(str){ 
  40.                   Alert.show(loginMess.username+",欢迎您回来...","提示"); 
  41.                   aaa.text=loginMess.username+",欢迎归来..."; 
  42.                   bbb.text=""; 
  43.                   login.label=""; 
  44.               }else{ 
  45.                   Alert.show("登录失败,您输入的用户名或者密码不存在!","提示"); 
  46.               } 
  47.               
  48.            } 
  49.            
  50.            protected function remoteObj_faultHandler(event:FaultEvent):void 
  51.            { 
  52.               // TODOAuto-generated method stub 
  53.               Alert.show(event.fault.message,"出错了"); 
  54.            } 
  55.            
  56.        ]]>  
  57.     </fx:Script>  
  58.      
  59.     <fx:Declarations>  
  60.        <!-- Place non-visualelements (e.g., services, value objects) here -->  
  61.        <s:RemoteObject id="remoteObj" destination="remoteObjectDemo"result="remoteObj_resultHandler(event)"fault="remoteObj_faultHandler(event)" />  
  62.     </fx:Declarations>  
  63.      
  64.        <s:Label x="219" y="150" width="182" height="27" fontSize="18" id="aaa" text="您还没有登陆,现在就" verticalAlign="middle"/>  
  65.        <mx:LinkButton x="409"  y="150" width="57"  height="27" label="登陆" id="login" fontSize="18"click="login_clickHandler(event)"/>  
  66.        <s:Label x="478" y="150" width="37" height="27" id="bbb" fontSize="18" text="吧!" verticalAlign="middle"/>  
  67.         
  68. </s:Application>  


 

        好了,页面与类算是处理完了,打开服务器并部署项目,运行felx页面RemoteObjectDemo.mxml,如下所示:




        当我们点击“登陆”按钮后,弹出module页面,如下所示:




      当我们输入的用户名和密码都正确时则提示你登陆正确:





       输入错误则提示你输入不正确:




       可以看出,我们输入的用户名与密码与Java中的login方法进行了交互。


        好了,就学习这么多,下节将学习HttpService方式。



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值