加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex 与 Java通信 RemoteObject 方式

发布时间:2020-12-15 05:06:27 所属栏目:百科 来源:网络整理
导读:原文出自: http://blog.csdn.net/youqishini/article/details/7462002 在学习了flash中的事件机制后,我们就开始学习flex与Java中的3种通信方式。Flex与Java通信有3种方式: ? ? ? ? ●flex访问Java普通类使用 RemoteObjec t方式,这也是用的最多的一种方式

原文出自: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. }??


?

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


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


?

?

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


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




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


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


?
    ????public?class?LoginMess??
  1. ????{??
  2. ???????private??var?_username:String;??
  3. ???????private?var?_passworld:String;??
  4. ???????public?function?LoginMess()??
  5. ????????????
  6. ???????public?function?get?passworld():String??
  7. ???????????return?_passworld;??
  8. ???????public?function?set?passworld(value:String):void??
  9. ???????????_passworld?=?value;??
  10. ???????public?function?get?username():String??
  11. ???????????return?_username;??
  12. ???????public?function?set?username(value:String):void??
  13. ???????????_username?=?value;??
  14. }??


?

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

?

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


?

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


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

?

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


?

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




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




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





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




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


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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读