?这个demo也是根据程序从前台页面执行到后台java程序的流程来书写的
注:在此程序中引入了blazeds
1. 页面中的组件元素
<s:HGroup>?
??????? <s:TextInput id="username" width="120"/>?
?????????
??????? <s:Button label="clickMe" click="clickMe()"/>?
</s:HGroup>?
<s:HGroup>
<s:TextInput id="username" width="120"/>
<s:Button label="clickMe" click="clickMe()"/>
</s:HGroup>
2. 实现click属性中的方法clickMe()函数
<fx:Script>?
??????? <![CDATA[
??????????? private function clickMe():void{
??????????????? var value:String = this.username.text;????????????????
??????????????? this.testService.sayHello(value);
??????????? }
??????? ]]>?
??? </fx:Script>?
<fx:Script>
?? 注意:在clickMe()函数中用到了远程对象testService,以及远程对象中的方法sayHello(),在书写该方法时并不会给出提示。
3. 在页面中引入远程对象testService
<fx:Declarations>?
??????? <!-- 将非可视元素(例如服务、值对象)放在此处 -->?
??????? <s:RemoteObject id="testService" destination="testService">?
??????????? <s:method name="sayHello" result="returnResultHandler(event)"/>?
??????? </s:RemoteObject>?
</fx:Declarations>?
解释:
?? 1)destination属性引入在remoting-config.xml文件中配置的<destination>标签中的对象名(该xml文件在下面的5部分给出)
?? 2) <s:RemoteObject>中的id表示给该对象起个唯一标识名称。
?? 3)<s:method>中name属性列出的是在远程对象中方法的名称,result属性是声明一个回调函数来处理结果值,该结果值在参数event中有封装。
4. 介绍处理返回结果的处理函数returnResultHandler(event)
view plaincopy to clipboardprint?<fx:Script>?
??????? <![CDATA[
??????????? import mx.controls.Alert;
??????????? import mx.rpc.events.ResultEvent;??????????????
?????????????
??????????? private function returnResultHandler(event:ResultEvent):void{
??????????????? var str:String = String(event.result);??????????????????
??????????????? Alert.show(str);
??????????? }
??????? ]]>?
??? </fx:Script>?
解释:1)event.result得到远程对象的结果返回值,并转换成String类型,并复制给str
??????????? 2)弹出对话框并输出str变量的值 Alert.show(str);
5. remoting-config.xml文件里暴漏出java对象供flex页面使用 <service id="remoting-service"?? ??? class="flex.messaging.services.RemotingService">? ????? ??? <destination id="testService">? ??????? <properties>? ??????????? <source>com.test.service.TestService</source>? ??????? </properties>? ??? </destination>? ? </service>? ? 注意:在书写该文件的过程中并没有提示功能,大家输入的时候,请多注意!最好是copy 6.对应该配置文件中的远程对象com.test.service.TestService 类的文件如下: view plaincopy to clipboardprint?public class TestService {? ? ??? public String sayHello(String name){? ????????? ??????? System.out.println("method=sayHello");? ??????? return "hello:"+name;? ??? }? }? public class TestService { public String sayHello(String name){ System.out.println("method=sayHello"); return "hello:"+name; } } 以上就是一个flex+java交互的小例子,该交互是通过Remote Object方法来完成!