flex与java通信的小例子

很简单的小例子,适合初学者理解

1,JavaFlex.java

  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

  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

  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>

源码下载

转载于:https://www.cnblogs.com/nianshi/archive/2010/05/11/1732447.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值