终于轮到Flex登场了
打开Flex Builder3新建一个Flex Project,在libs里面加入PureMVC_AS3_2_0_4.swc,导入puremvc框架,
图片是我这个项目的结构,对Puremvc不了解的朋友还请多看看相关的教程,推荐文章http://flex2.group.iteye.com/group/blog/212110
这里我只说关键的与Java后端的交互,我在util包里新建一个Class来创建RemoteObject
package com.util
{
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.remoting.RemoteObject;
public class CreateRemoteObject
{
public function create(roDestination:String, channelURI:String, channelId:String):RemoteObject {
var channelSet:ChannelSet = new ChannelSet();
var channel:AMFChannel = new AMFChannel(channelId, channelURI);
channelSet.addChannel(channel);
var ro:RemoteObject = new RemoteObject(roDestination);
ro.channelSet = channelSet;
return ro;
}
}
}
然后在Proxy里面调用
//登陆
public function login(user:User):void{
var result:Boolean = false;
var create:CreateRemoteObject = new CreateRemoteObject();
var ro:RemoteObject = create.create("userService","http://localhost:8080/zhangben/spring/messagebroker/amf","c1");
ro.userLogin(user.username,user.password);
ro.addEventListener(ResultEvent.RESULT,onLoginResult);
ro.addEventListener(FaultEvent.FAULT,onFault);
}
看到没有这里ro.userLogin(user.username,user.password); 就能直接调用Spring暴露的userLogin方法,然后在onLoginResult这个回调函数里面能够得到这个方法的返回值。具体代码参考最后的源码
结束语:我搭建这整套应用前后差不多用了一周的时间,当然每个人的习惯不同。有人可能不喜欢用Maven,或者有人觉得Puremvc没太大用处。我也不是什么高手,在此之中肯定还有许多遗漏和错误,肯请朋友指出,小弟虚心接受更正。
我不怕板砖,只怕你拍的不够狠!