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

flex 学习笔记 RemoteObject(1)

发布时间:2020-12-15 04:54:44 所属栏目:百科 来源:网络整理
导读:RemoteObject 类使您可以访问远程应用程序服务器上的类。 Flex支持多种与服务器端的通讯方式,包括remote和socket等高级数据通讯方式。 remote采用amf(action message format)协议。amt是一种二进制格式,专用于as和服务器端通讯,比http通讯要快很多,而

RemoteObject 类使您可以访问远程应用程序服务器上的类。

Flex支持多种与服务器端的通讯方式,包括remote和socket等高级数据通讯方式。 remote采用amf(action message format)协议。amt是一种二进制格式,专用于as和服务器端通讯,比http通讯要快很多,而且支持多种数据类型,如java,.net,php等。本文将介绍如何使用Flex RemoteObject components调用服务器端java对象的方法。

Flex客户端使用RemoteObject

在mxml中声明一个RemoteObject,在as中可以通过id引用RemoteObject对象,destination是服务器端暴露的java 服务对象,Method的name是java服务对象中的方法,result是访问服务器方法的回调函数。

testHelloWorld.mxml代码

?

 
 
  1. <?xml?version=”1.0″?encoding=”utf-8″?>?
  2. ?
  3. <mx:Application?xmlns:mx=”http://www.adobe.com/2006/mxml”>?
  4. ?
  5. <mx:Script>?
  6. ?
  7. <![CDATA[ ?
  8. ?
  9. import?mx.controls.Alert; ?
  10. ?
  11. import?mx.rpc.events.ResultEvent; ?
  12. ?
  13. [Bindable]? ?
  14. ?
  15. private?var?memberResult:Object; ?
  16. ?
  17. ?
  18. private?function?say():void{ ?
  19. ?
  20. var?user:User=new?User(); ?
  21. ?
  22. user.setName(n.text); ?
  23. ?
  24. user.setId(”testId”); ?
  25. ?
  26. ro.sayHello(user); ?
  27. ?
  28. } ?
  29. public?function?handleResult(event:ResultEvent):void?{ ?
  30. ?
  31. target.text=event.result?as?String; ?
  32. ?
  33. } ?
  34. ?
  35. ]]>?
  36. ?
  37. </mx:Script>?
  38. ?
  39. <mx:RemoteObject?id=”ro”?destination=”HelloWorld”>?
  40. ?
  41. <mx:method?name=”?sayHello?”?result=”?handleResult?(event)”/>?
  42. ?
  43. <mx:RemoteObject>?
  44. ?
  45. <mx:TextInput?id=”n”?change=”say()”/>? ?
  46. ?
  47. <mx:Label?id=”target”/>?
  48. ?
  49. </mx:Application>?
  50. ?

User.as代码

 
 
  1. package?{ ?
  2. ?
  3. [RemoteClass(alias="cn.com.?remote.test.User")] ?
  4. public?class?User?{? ?
  5. ?
  6. public?var?name:String; ?
  7. ?
  8. public?var?id:String; ?
  9. ?
  10. ?
  11. public?function?getName():String{ ?
  12. ?
  13. return?name; ?
  14. ?
  15. } ?
  16. ?
  17. public?function?setName(name:String?):void?{ ?
  18. ?
  19. this.name?=?name; ?
  20. ?
  21. } ?
  22. ?
  23. public?function?getId():String?{ ?
  24. ?
  25. return?id; ?
  26. ?
  27. } ?
  28. ?
  29. public?function?setId(id:String):void?{ ?
  30. ?
  31. this.id?=?id; ?
  32. ?
  33. } ?
  34. ?
  35. } ?
  36. ?
  37. } ?
  38. ?
定义服务器端java对象

HelloWorld.java代码

?

 
 
  1. package?cn.com.?remote.test; ?
  2. ?
  3. ?
  4. public?class?HelloWorld?{ ?
  5. ?
  6. public?String?sayHello(String?name){ ?
  7. ?
  8. System.out.println(”**********sayHello(String?name)?in*************”); ?
  9. ?
  10. System.out.println(”hello,”+name); ?
  11. ?
  12. return?“hello,”+name; ?
  13. ?
  14. } ?
  15. ?
  16. ?
  17. public?User?getUser(String?name){ ?
  18. ?
  19. System.out.println(”**********getUser?in*************”); ?
  20. ?
  21. return?new?User(name,name+”Id”); ?
  22. ?
  23. } ?
  24. ?
  25. } ?
  26. ?

User.java代码

 
 
  1. package?cn.com.?remote.test; ?
  2. ?
  3. ?
  4. public?class?User?{ ?
  5. ?
  6. private?String?name; ?
  7. ?
  8. private?String?id; ?
  9. ?
  10. public?String?getName()?{ ?
  11. ?
  12. return?name; ?
  13. ?
  14. } ?
  15. ?
  16. public?void?setName(String?name)?{ ?
  17. ?
  18. this.name?=?name; ?
  19. ?
  20. } ?
  21. ?
  22. public?String?getId()?{ ?
  23. ?
  24. return?id; ?
  25. ?
  26. } ?
  27. ?
  28. public?void?setId(String?id)?{ ?
  29. ?
  30. this.id?=?id; ?
  31. ?
  32. } ?
  33. ?
  34. } ?

(编辑:李大同)

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

    推荐文章
      热点阅读