flex+blazeDS+java示例

开发环境:

1. eclispe 3.5
2. Flex builder 3.0(eclipse plugin)
3. BlazeDS 3.2
4. Tomcat6.0

操作步骤
1. 从BlazeDS.war建立项目
选择菜单File->import,指定类型为web/war file.下一步选择BlazeDS.war,项目名称为test-server,target-runtime指定为tomcat6,按Finish结束。

2. 建立java类,代码如下:

package remoting;

public class EchoService {
public String echo(String text) {
return "Server says: I received '" + text + "' from you";
}
}

3. 设置amf channel
编辑WEB-INF/flex/services-config.xml,找到amf channel定义的地方,配置如下:

<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://localhost:8080/test-server/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>false</polling-enabled>
</properties>
</channel-definition>

4. 定义一个destination:
编辑WEB-INF/flex/remoting-config.xml,增加destination定义如下:

<destination id="echoServiceDestination" channels="my-amf">
<properties>
<source>remoting.EchoService</source>
</properties>
</destination>

5. 给项目增加flex特性
鼠标右键点击项目,选择Flex project nature->Add Flex project nature

6. 编辑客户端代码
输入客户端代码如下:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">

<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

// Send the message in response to a Button click.
private function echo():void {
var text:String = ti.text;
remoteObject.echo(text);
}

// Handle the recevied message.
private function resultHandler(event:ResultEvent):void {
ta.text += "Server responded: "+ event.result + "\n";
}

// Handle a message fault.
private function faultHandler(event:FaultEvent):void {
ta.text += "Received fault: " + event.fault + "\n";
}
]]>
</mx:Script>

<mx:RemoteObject
id="remoteObject"
destination="echoServiceDestination"
endpoint="http://localhost:8080/test-server/messagebroker/amf"
result="resultHandler(event);"
fault="faultHandler(event);"/>
<mx:Label text="Enter a text for the server to echo"/>
<mx:TextInput id="ti" text="Hello World!"/>
<mx:Button label="Send" click="echo();"/>
<mx:TextArea id="ta" width="100%" height="100%"/>

</mx:Application>

7. 运行
在Eclipse里运行:确认该项目已经加到tomcat6 server中,启动server.右键点击mxml文件,选择Run As->Flex Application,将会打开一个新的浏览器窗口,其中可以看到运行的程序.
也可以把项目export到war文件,发布到tomcat或其他应用服务器上.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值