flex与java通信的小例子
很简单的小例子,适合初学者理解
1,JavaFlex.java
- package flex;
- public class JavaFlex {
- public String helloJavaFlex(String name) {
- return name;
- }
- }
2,remoting-config.xml
- <?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
- <?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>