原文出自: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类,代码如下所示:
- package?com.yqsn.test;??
- ???
- public?class?RemoteObjectDemo?{??
- ????public?boolean?login(String?username,String?passworld?){??
- ????????
- ???????if(username.equals("admin")&&passworld.equals("123")){??
- ???????????return?true;??
- ???????}else{??
- ???????????return?false;??
- ???????}??
- ????}??
- }??
?
? ? ? ?在WebRoot/WEB-INF/flex目录下的remoting-config.xml文件中添加如下所示代码:
<destination?id="remoteObjectDemo">??
- ???????<properties>??
- ???????????<source>com.yqsn.test.RemoteObjectDemo</source>??
- ???????</properties>??
- ????</destination>??
?
?
? ? ? ?将MyEclipse切换到Flash视图,首先自定义一个事件类LoginEvent.as,为以后传值服务,代码如下所示:
package?com.flex.ases??
- {??
- ????import?flash.events.Event;??
- ?????
- ????public?class?LoginEvent?extends?Event??
- ????{????
- ???????public?static?const?LOGIN_EVENT:String="LOGIN_EVENT";??
- ????????
- ???????private??var?_loginMess:Object;??
- ???????public?function?LoginEvent(type:String,loginMess:Object=null,?bubbles:Boolean=false,cancelable:Boolean=false)??
- ???????{??
- ???????????this._loginMess=loginMess;??
- ???????????super(type,?bubbles,?cancelable);??
- ???????}??
- ???????public?function?get?loginMess():Object??
- ???????????return?_loginMess;??
- ???
- ???????public?function?set?loginMess(value:Object):void??
- ???????{??
- ???????????_loginMess?=?value;??
- ????}??
- }??
? ? ?在这个类中我们定义了一个事件类型LOGIN_EVENT,定义了一个Object类型的变量,用于存值。
? ? ?接着新建一个登陆信息的VO类LoginMess.as,为以后存贮用户信息服务,代码如下所示:
????public?class?LoginMess??
- ????{??
- ???????private??var?_username:String;??
- ???????private?var?_passworld:String;??
- ???????public?function?LoginMess()??
- ????????????
- ???????public?function?get?passworld():String??
- ???????????return?_passworld;??
- ???????public?function?set?passworld(value:String):void??
- ???????????_passworld?=?value;??
- ???????public?function?get?username():String??
- ???????????return?_username;??
- ???????public?function?set?username(value:String):void??
- ???????????_username?=?value;??
- }??
?
? ? ? ? 新建一个用于登陆的MXMLModule文件LoginModule.mxml,代码如下所示:
?
<?xmlversionxmlversion="1.0"?encoding="utf-8"?>??
- <s:Module?xmlns:fx="http://ns.adobe.com/mxml/2009"??
- ?????????xmlns:s="library://ns.adobe.com/flex/spark"??
- ?????????xmlns:mx="library://ns.adobe.com/flex/mx"?width="256"?height="213">??
- ?????
- ????<fx:Script>??
- ???????<![CDATA[?
- ???????????import?com.flex.ases.LoginEvent;?
- ???????????
- ???????????import?mx.controls.Alert;?
- ???????????import?mx.events.CloseEvent;?
- ???????????import?mx.managers.PopUpManager;?
- ???????????protected?function?login_clickHandler(event:MouseEvent):void?
- ???????????{?
- ??????????????//?TODOAuto-generated?method?stub?
- ??????????????var?loginMess:Object=new?Object;?
- ??????????????loginMess.username=userName.text;?
- ??????????????loginMess.passworld=passworld.text;?
- ??????????????if(userName.text==""?||passworld.text==""){?
- ??????????????????Alert.show("用户名或密码不能为空!");?
- ??????????????????return;?
- ??????????????}?
- ??????????????this.dispatchEvent(newLoginEvent(LoginEvent.LOGIN_EVENT,loginMess));?
- ??????????????userName.text="";?
- ??????????????passworld.text="";?
- ??????????????PopUpManager.removePopUp(this);?
- ???????????}?
- ???????????protected?function?loginTitleWindow_closeHandler(event:CloseEvent):void?
- ??????????????//?TODO?Auto-generatedmethod?stub?
- ???????]]>??
- ????</fx:Script>??
- ????<fx:Declarations>??
- ?????????
- ????</fx:Declarations>??
- ????????<s:TitleWindow?x="1"?y="1"?width="256"height="213"?title="登陆"id="loginTitleWindow"?close="loginTitleWindow_closeHandler(event)"?>??
- ???????<s:Form?width="100%"?height="183"?>??
- ???????????<s:FormItem?left="60"?height="39"?width="224"?label="用户名"?required="true"??>??
- ??????????????<s:TextInput?id="userName"?/>??
- ???????????</s:FormItem>??
- ???????????<s:FormItem?required="true"?width="224"?label="密码"?>??
- ??????????????<s:TextInput?id="passworld"?displayAsPassword="true"?/>??
- ???????????</s:FormItem>??????
- ???????????<s:FormItem?width="227">??
- ??????????????<s:Button?id="login"?label="登陆"?click="login_clickHandler(event)"/>??
- ???????</s:Form>??
- ???????</s:TitleWindow>???
- </s:Module>??
?
? ? ? ? 这个页面以后我们反复使用,这就是module文件的优点之一。在这个页面中我们不处理与Java交互的部分,因为既然是公共页面,我们应该将于Java交互的部分放在相应引用的文件中。
? ? ? ? ?接着创建主页面RemoteObjectDemo.mxml,代码如下所示:
?
<?xml?version="1.0"encoding="utf-8"?>??
- <s:Application?xmlns:fx="http://ns.adobe.com/mxml/2009"??
- ??????????????xmlns:s="library://ns.adobe.com/flex/spark"??
- ??????????????xmlns:mx="library://ns.adobe.com/flex/mx"?width="100%"?height="100%">??
- ???????????import?com.flex.ases.LoginMess;?
- ???????????import?com.flex.component.LoginTitleWindow;?
- ???????????import?com.flex.module.LoginModule;?
- ???????????
- ???????????import?mx.collections.ArrayCollection;?
- ???????????import?mx.managers.PopUpManager;?
- ???????????import?mx.rpc.events.FaultEvent;?
- ???????????import?mx.rpc.events.ResultEvent;?
- ???????????[Bindable]?
- ???????????private?var?loginMess:LoginMess=new?LoginMess();?
- ???????????private?var?loginModule:LoginModule=new?LoginModule();?
- ???????????protected?function?login_clickHandler(event:MouseEvent):void?
- ???????????{?
- ??????????????PopUpManager.addPopUp(loginModule,this,true);?
- ??????????????PopUpManager.centerPopUp(loginModule);?
- ??????????????loginModule.addEventListener(LoginEvent.LOGIN_EVENT,getLoginMess);?
- ???????????public?function?getLoginMess(event:LoginEvent):void{?
- ??????????????var?username:String=event.loginMess['username'];?
- ??????????????var?passworld:String=event.loginMess['passworld'];?
- ??????????????loginMess.username=username;?
- ??????????????remoteObj.login(username,passworld);?
- ??????????????
- ???????????}?
- ???????????protected?function?remoteObj_resultHandler(event:ResultEvent):void?
- ??????????????//?TODOAuto-generated?method?stub?
- ??????????????var?str:Boolean=event.result?as?Boolean;?
- ??????????????if(str){?
- ??????????????????Alert.show(loginMess.username+",欢迎您回来...","提示");?
- ??????????????????aaa.text=loginMess.username+",欢迎归来...";?
- ??????????????????bbb.text="";?
- ??????????????????login.label="";?
- ??????????????}else{?
- ??????????????????Alert.show("登录失败,您输入的用户名或者密码不存在!","提示");?
- ??????????????}?
- ??????????????
- ???????????protected?function?remoteObj_faultHandler(event:FaultEvent):void?
- ??????????????Alert.show(event.fault.message,"出错了");?
- ???????<s:RemoteObject?id="remoteObj"?destination="remoteObjectDemo"result="remoteObj_resultHandler(event)"fault="remoteObj_faultHandler(event)"?/>??
- ????</fx:Declarations>??
- ???????<s:Label?x="219"?y="150"?width="182"?height="27"?fontSize="18"?id="aaa"?text="您还没有登陆,现在就"?verticalAlign="middle"/>??
- ???????<mx:LinkButton?x="409"??y="150"?width="57"??height="27"?label="登陆"?id="login"?fontSize="18"click="login_clickHandler(event)"/>??
- ???????<s:Label?x="478"?y="150"?width="37"?height="27"?id="bbb"?fontSize="18"?text="吧!"?verticalAlign="middle"/>??
- </s:Application>??
?
? ? ? ? 好了,页面与类算是处理完了,打开服务器并部署项目,运行felx页面RemoteObjectDemo.mxml,如下所示:

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

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

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

? ? ? ?可以看出,我们输入的用户名与密码与Java中的login方法进行了交互。
? ? ? ? 好了,就学习这么多,下节将学习HttpService方式。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|