您的位置:首页 > 编程语言 > Java开发

flex通过BlazeDS与java后台通信

2015-01-08 16:18 267 查看
1.通过WTP创建BlazeDS的Flex项目,可参考

http://note.youdao.com/share/?id=8455692f104b5610131f8befe9f4c681&type=note

2.添加后台service类,如下

package com.service;
public class Test {
public String testFlex(String ars){
return "Hello "+ars;
}
}


3.修改remoting-config.xml文件中添加以下代码

<destination id="myTest">
<properties>
<source>com.service.Test</source>
</properties>
</destination>


4.前台Flex调用方法

<?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:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
protected function button1_clickHandler(event:MouseEvent):void
{
var remoObj:RemoteObject=new RemoteObject();
remoObj.destination="myTest";
remoObj.testFlex("suxiaoyong");
remoObj.addEventListener(FaultEvent.FAULT,fieldFunction);
remoObj.addEventListener(ResultEvent.RESULT,resultFunction);
remoObj.showBusyCursor=true;
}

//调用失败后返回
private function fieldFunction(e:FaultEvent):void{

Alert.show("调用失败"+e.fault.toString());
}
//调用成功后返回
private function resultFunction(e:ResultEvent):void{
var result:String=e.result.toString()
Alert.show(result);
}
]]>
</fx:Script>

<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Button label="点击" click="button1_clickHandler(event)"/>
</s:Application>


5.如果出现错误如下图:



导致原因是url不对。解决:修改.flexProperties文件中的,serverContextRoot为项目的名称。

6.若出现以下界面,则说明调用成功。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: