flex和java交互的三种方法之二:Remote Object
这个demo也是根据程序从前台页面执行到后台java程序的流程来书写的 注:在此程序中引入了blazeds 引入blazeds的方法: 在Myeclipse的web项目中 右键点击WebRoot-->Import-->File System-->在From directory中找到自己的blazeds解压后的文件--->finish。 1. 页面中的组件元素 <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>注意:在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部分给出)
4. 介绍处理返回结果的处理函数returnResultHandler(event) <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
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 类的文件如下: public class TestService { public String sayHello(String name){ System.out.println("method=sayHello"); return "hello:"+name; } } 以上就是一个flex+java交互的小例子,该交互是通过Remote Object方法来完成! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |