Flex与Java中的Object交互

开发 后端
Flex与Java中的Object进行交互,需要准备三个内容,一个是Flex主文件,也就是我们的mxml文件,两个Java类,一个remoting-config.xml。为了减少程序的复杂性,这里只证明Flex可以调用 Java中Object进行交互,不进行与数据库的交互。

Flex与Java中的Object进行交互,需要准备三个内容,一个是Flex主文件,也就是我们的mxml文件,两个Java类,一个remoting-config.xml。为了减少程序的复杂性,这里只证明Flex可以调用 Java中Object进行交互,不进行与数据库的交互。

新建一个Java实体类:SpeakUser.java, 如下:

  1. package com.cx.model; 
  2.  
  3. import java.io.Serializable; 
  4.  
  5. public class SpeakUser implements Serializable{ 
  6.          
  7.     private static final long serialVersionUID = -4154296753553491429L; 
  8.  
  9.     private String userName; 
  10.      
  11.     private String userPwd; 
  12.      
  13.     private String userTel; 
  14.      
  15.     private String userMail; 
  16.  
  17.     public String getUserName() { 
  18.         return userName; 
  19.     } 
  20.  
  21.     public void setUserName(String userName) { 
  22.         this.userName = userName; 
  23.     } 
  24.  
  25.     public String getUserPwd() { 
  26.         return userPwd; 
  27.     } 
  28.  
  29.     public void setUserPwd(String userPwd) { 
  30.         this.userPwd = userPwd; 
  31.     } 
  32.  
  33.     public String getUserTel() { 
  34.         return userTel; 
  35.     } 
  36.  
  37.     public void setUserTel(String userTel) { 
  38.         this.userTel = userTel; 
  39.     } 
  40.  
  41.     public String getUserMail() { 
  42.         return userMail; 
  43.     } 
  44.  
  45.     public void setUserMail(String userMail) { 
  46.         this.userMail = userMail; 
  47.     } 
  48.  

有的作者说这个实体类需要当做Java代码中方法的返回值传递给Flex中的AS代码,所以需要继承Serializable接口,否则将导致异常的发生。但是经过笔者测试,这个Serializable是可选的,即使不继承,也不会出现什么问题,可能是由于flex的版本所决定的吧。

再创建一Java对象:IntroduceOneself.java,供Flex调用, 类中 以SpeakUser作为参数。代码如下:

  1. package com.cx.action; 
  2.  
  3. import com.cx.model.*; 
  4.  
  5. public class IntroduceOneself { 
  6.  
  7.     public String speak(SpeakUser user){ 
  8.         System.out.println("Hello, My Name is: " + user.getUserName()); 
  9.         System.out.println("My telephone is: " + user.getUserTel()); 
  10.         System.out.println("My Email is: " + user.getUserMail());        
  11.         return "名字叫:" + user.getUserName() + ", 电话是: "  
  12.                 + user.getUserTel() + ", 邮箱是: "+user.getUserMail()  
  13.                 + "来访问您,请问您是否要接见?"
  14.     } 

然后创建一个ActionScript类:SpeakUser.as, 用于接受实体类SpeakUser.java的返回值,这里的[Bindable][RemoteClass]是和java代码中的 SpeakUser.java类关联起来。这样利用类型转换将java对象转换成AS对象。代码如下:

  1. package com.flex.model 
  2.      
  3.     [Bindable] 
  4.     [RemoteClass(alias="com.cx.model.SpeakUser")] 
  5.     public class SpeakUser 
  6.     { 
  7.         public var userName:String=""
  8.         public var userPwd:String =""
  9.         public var userTel:String =""
  10.         public var userMail:String =""
  11.                  
  12.         public function SpeakUser() 
  13.         { 
  14.              
  15.         } 
  16.     } 

修改Flex主文件:BlazObject.mxml,在其中调用java类,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"> 
  3.      
  4.     <mx:Script> 
  5.         <![CDATA[            
  6.             import com.flex.model.SpeakUser; 
  7.              
  8.             import mx.controls.Alert; 
  9.             import mx.rpc.events.ResultEvent; 
  10.              
  11.             public function remotingHelloJavaFlex():void 
  12.             { 
  13.                  
  14.                 var user:SpeakUser = new SpeakUser(); 
  15.              
  16.                 user.userName = nameInputTxt.text; 
  17.                 user.userMail = txtUserMail.text; 
  18.                 user.userPwd = txtPwd.text; 
  19.                 user.userTel = txtTel.text; 
  20.                  
  21.                                  
  22.                 someOneCome.speak(user); 
  23.                 someOneCome.addEventListener(ResultEvent.RESULT,getRoHelloRes); 
  24.             } 
  25.              
  26.             private function getRoHelloRes(e:ResultEvent):void{ 
  27.                 Alert.show(e.result.toString()); 
  28.             } 
  29.              
  30.             private function failed():void{ 
  31.                 Alert.show("cuowu"); 
  32.             } 
  33.         ]]> 
  34.     </mx:Script> 
  35.      
  36.     <mx:Button label="JAVA Object + FLEX 通信" click="remotingHelloJavaFlex();" x="142" y="234" 
  37.                fontSize="12" width="209"/> 
  38.     <mx:Label text="姓名" x="121" y="55" fontSize="15"/> 
  39.     <mx:TextInput id="nameInputTxt" x="170" y="55"/> 
  40.     <mx:Label x="121" y="103" fontSize="15" text="密码:"/> 
  41.     <mx:TextInput id = "txtPwd" x="170" y="106"/> 
  42.     <mx:Label x="121" y="145" fontSize="15" text="电话:"/> 
  43.     <mx:Label x="121" y="187" fontSize="15" text="邮箱:"/> 
  44.     <mx:TextInput id="txtTel" x="170" y="148"/> 
  45.     <mx:TextInput id="txtUserMail" x="170" y="190"/> 
  46.      
  47.     <mx:RemoteObject destination="someOneComeDes" id="someOneCome" endpoint="/BlazDSObject/messagebroker/amf" fault="failed()" /> 
  48.      
  49. </mx:Application> 

在这里,Flex是通过AMF协议与Java对象进行交互的,其中endpoint定义了影射文件的消息协议,这个文件来自/WEB-INF/flex/services-config.xml中,影射对象的位置是通过remoting- service.xml影射的代码如下:

  1. <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel"> 
  2.             <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/> 
  3.             <properties> 
  4.                 <add-no-cache-headers>false</add-no-cache-headers> 
  5.             </properties> 
  6.         </channel-definition> 

remoting-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <service id="remoting-service"  
  3.     class="flex.messaging.services.RemotingService"> 
  4.  
  5.     <adapters> 
  6.         <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/> 
  7.     </adapters> 
  8.  
  9.     <default-channels> 
  10.         <channel ref="my-amf"/> 
  11.     </default-channels> 
  12.      
  13.     <destination id="someOneComeDes"> 
  14.         <properties> 
  15.             <source> 
  16.                 com.cx.action.IntroduceOneself 
  17.             </source> 
  18.         </properties> 
  19.     </destination> 
  20.      
  21. </service> 

原文链接:http://blog.csdn.net/shehun1/article/details/6691462

【编辑推荐】

  1. Java 8整装待发 图谋云计算
  2. Java 7是蜜糖还是毒药?
  3. 选用Ibatis和Hibernate的区别
  4. JRuby和Java 7 我们可以期待什么
  5. Flex前端与Java服务端交互,反射机制挑大旗

 

 

 

 

 

 

 

责任编辑:艾婧 来源: shehun1的博客
相关推荐

2010-07-30 12:56:02

Flex调用JavaS

2009-07-06 10:41:38

flex与servle

2010-08-10 17:01:48

FlexJavaScript

2010-07-27 12:28:56

Flex Remote

2011-08-17 09:14:03

FlexJava

2010-08-10 09:40:23

Flex与浏览器交互

2010-08-06 13:59:00

Flex和Html交互

2010-08-03 14:24:37

Flex与java集成

2022-05-16 11:17:01

应用开发JSJAVA

2010-07-30 13:25:36

Flex

2010-07-27 15:36:15

Flex swf

2010-08-09 15:41:13

Flex字体

2010-07-29 14:27:52

FlexWeb

2010-08-06 09:24:44

FlexFlash

2021-03-25 15:54:14

鸿蒙HarmonyOS应用开发

2021-09-14 18:33:39

React 数据交互

2010-07-08 10:13:54

UML交互图

2010-08-09 15:19:29

Flex滚动条

2020-06-04 08:13:36

JavaScriptObject.is()运算符

2010-07-02 13:22:25

UML交互图
点赞
收藏

51CTO技术栈公众号