flex與java通訊的小例子

來源:互聯網
上載者:User

很簡單的小例子,適合初學者理解

1,JavaFlex.java

view plaincopy to clipboardprint?
  1. package flex;  
  2.   
  3. public class JavaFlex {  
  4.     public String helloJavaFlex(String name) {  
  5.         return name;  
  6.     }  
  7. }  

package flex; public class JavaFlex { public String helloJavaFlex(String name) { return name; } }

2,remoting-config.xml

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

<?xml version="1.0" encoding="UTF-8"?> <service id="remoting-service" class="flex.messaging.services.RemotingService"> <adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true" /> </adapters> <default-channels> <channel ref="my-amf" /> </default-channels> <destination id="firstJavaFlex"> <properties> <source>flex.JavaFlex </source> </properties> </destination> </service>

3,testflex.mxml

view plaincopy to clipboardprint?
  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" minWidth="955" minHeight="600">  
  5.     <fx:Declarations>  
  6.         <!-- 將 非可視元素(例如服務、值對象)放在此處 -->  
  7.         <s:RemoteObject id="selectHello" destination="firstJavaFlex" fault="error(event)"/>  
  8.     </fx:Declarations>  
  9.     <fx:Script>  
  10.         <!--[CDATA[  
  11.             import mx.collections.ArrayCollection;  
  12.             import mx.controls.Alert;  
  13.             import mx.events.ResizeEvent;  
  14.             import mx.rpc.events.FaultEvent;  
  15.             import mx.rpc.events.ResultEvent;  
  16.               
  17.             [Bindable]  
  18.             private var arraylist:ArrayCollection = new ArrayCollection([  
  19.                 {name:"你好",data:"你好!"},  
  20.                 {name:"你們好",data:"你們 好!"},  
  21.                 {name:"大家好",data:"大家 好!"},  
  22.             ]);  
  23.             private function changehandler(event:Event):void{  
  24.                 selectHello.helloJavaFlex(selected.selectedItem.data);  
  25.                 selectHello.addEventListener(ResultEvent.RESULT,remoteResult);  
  26.             }  
  27.             private function remoteResult(event:ResultEvent):void{  
  28.                 Alert.show(event.result.toString());  
  29.                 resultmess.text = event.result.toString();  
  30.             }  
  31.             private function error(event:FaultEvent):void{  
  32.                 Alert.show(event.message.body.toString());  
  33.                 resultmess.text = event.message.body.toString();  
  34.             }  
  35.         ]]-->  
  36.     </fx:Script>  
  37.     <s:DropDownList x="113" y="83" id="selected" labelField="name" dataProvider="{arraylist}" change="changehandler(event)"/>  
  38.     <s:RichText x="321" y="227" id="resultmess" text="RichText"/>  
  39. </s:Application>  

<?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" minWidth="955" minHeight="600"> <fx:Declarations> <!-- 將非可視元素(例如服務、值對象)放在此處 --> <s:RemoteObject id="selectHello" destination="firstJavaFlex" fault="error(event)"/> </fx:Declarations> <fx:Script> <!--[CDATA[ import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.events.ResizeEvent; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; [Bindable] private var arraylist:ArrayCollection = new ArrayCollection([ {name:"你好",data:"你好!"}, {name:"你們好",data:"你們好!"}, {name:"大家好",data:"大家好!"}, ]); private function changehandler(event:Event):void{ selectHello.helloJavaFlex(selected.selectedItem.data); selectHello.addEventListener(ResultEvent.RESULT,remoteResult); } private function remoteResult(event:ResultEvent):void{ Alert.show(event.result.toString()); resultmess.text = event.result.toString(); } private function error(event:FaultEvent):void{ Alert.show(event.message.body.toString()); resultmess.text = event.message.body.toString(); } ]]--> </fx:Script> <s:DropDownList x="113" y="83" id="selected" labelField="name" dataProvider="{arraylist}" change="changehandler(event)"/> <s:RichText x="321" y="227" id="resultmess" text="RichText"/> </s:Application>

源碼下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.