flex和java交互的三种方法之一:Remote Object

2014-11-24 02:53:07 · 作者: · 浏览: 0

这个demo也是根据程序从前台页面执行到后台java程序的流程来书写的


注:在此程序中引入了blazeds


1. 页面中的组件元素
view plaincopy to clipboardprint







2. 实现click属性中的方法clickMe()函数

view plaincopy to clipboardprint
private function clickMe():void{
var value:String = this.username.text;

this.testService.sayHello(value);
}
]]>


private function clickMe():void{
var value:String = this.username.text;

this.testService.sayHello(value);
}
]]>
注意:在clickMe()函数中用到了远程对象testService,以及远程对象中的方法sayHello(),在书写该方法时并不会给出提示。

3. 在页面中引入远程对象testService

view plaincopy to clipboardprint










解释:1)destination属性引入在remoting-config.xml文件中配置的标签中的对象名(该xml文件在下面的5部分给出)

2) 中的id表示给该对象起个唯一标识名称。

3)中name属性列出的是在远程对象中方法的名称,result属性是声明一个回调函数来处理结果值,该结果值在参数event中有封装。


4. 介绍处理返回结果的处理函数returnResultHandler(event)

view plaincopy to clipboardprint
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;


private function returnResultHandler(event:ResultEvent):void{
var str:String = String(event.result);

Alert.show(str);
}
]]>


import mx.controls.Alert;
import mx.rpc.events.ResultEvent;


private function returnResultHandler(event:ResultEvent):void{
var str:String = String(event.result);

Alert.show(str);
}
]]>
解释:1)event.result得到远程对象的结果返回值,并转换成String类型,并复制给str

2)弹出对话框并输出str变量的值 Alert.show(str);


5. remoting-config.xml文件里暴漏出java对象供flex页面使用

view plaincopy to clipboardprint class="flex.messaging.services.RemotingService">



com.test.service.TestService




class="flex.messaging.services.RemotingService">



com.test.service.TestService

注意:在书写该文件的过程中并没有提示功能,大家输入的时候,请多注意!最好是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方法来完成!

作者“liuwang8888”