1.采用BlazeDS进行java应用程序与flex应用程序间的通信。

(1)在官网上下载blazeds的开发包,最好下载完整版,里面有一个例子和所有我们所需的所有文件(我下载的是blazeds-turnkey-4.0.0.14931版)。将blazeds.war、ds-console.war、samples.war三个文件放在tomcat的webapps目录下

flex-messaging-common.jar

flex-messaging-core.jar

flex-messaging-opt.jar

flex-messaging-proxy.jar

flex-messaging-remoting.jar

backport-util-concurrent.jar

cfgatewayadapter.jar

commons-httpclient-3.0.1.jar

commons-codec-1.3.jar

commons-logging.jar

concurrent.jar

xalan.jar

(2)然后要加入Flex BlazeDS需要的配置文件。在WEB-INF下新建一个名为flex的文件夹,然后将四个xml文件(messagin-config.xml/proxy-config.xml/remoting-config.xml/services-config.xml)拷到该文件夹下。

(3)    修改web.xml文件,加入Flex的配置(直接拷贝即可)。

flex.class.path

/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars

flex.messaging.HttpFlexSession

MessageBrokerServlet

MessageBrokerServlet

flex.messaging.MessageBrokerServlet

services.configuration.file

/WEB-INF/flex/services-config.xml

1

MessageBrokerServlet

/messagebroker/*

以上步骤需在java项目中完成

2.Flex采用Cairngorm作为前端架构

(1)在Flex Builder中新建一J2EE项目,并配置服务器路径(在tomcat/webapp/目录下,需关联一web项目)

(2)在Flex Build Path中引入Cairngorm.swc,查看是否引用了正确的sdk包,如不正确,需重新引用。

(3)按照Cairngorm架构,在src目录下分别创建bussiness,command,event,model,view文件夹。

其中,view为视图层,model为模型层,event为事件,command为业务逻辑层,bussiness中需创建delegate(中间人角色,command调用web service获得数据时,需创建一个delegate完成,一对一关系),Front Controller(注册event和command的对应关系)和services.mxml(服务定义)。

具体调用过程如下:

n首先我们要了解我们的数据

n我们将使用数据来定义View

n我们将使用View来定义可能的用户动作

n用户动作将会转化(转变)为Events

nEvent将会被映射到Command

nCommand可能被映射到Delegate

nDelegate映射到Service

nCommand将使用Service传回的结果更新Model Locator

3.Flex和Spring整合

(1)首先,需要有一个加载bean的工厂类,我的项目中为SpringFactory.java,类中的代码固定

publicclassSpringFactoryimplementsFlexFactory{

//定义一个常量资源

privatestaticfinalStringSOURCE="source";

publicvoidinitialize(String id, ConfigMap configMap) {

}

/**

*Thismethodiscalledwhenweinitializethedefinitionofaninstance

*whichwillbelookedupbythisfactory.Itshouldvalidatethat

*thepropertiessuppliedarevalidtodefineaninstance.

*Anyvalidpropertiesusedforthisconfigurationmustbeaccessedto

*avoidwarningsaboutunusedconfigurationelements.Ifyourfactory

*isonlyusedforapplicationscopedcomponents,thismethodcansimply

*returnafactoryinstancewhichdelegatesthecreationofthecomponent

*totheFactoryInstance'slookupmethod.

*/

//创建一个factory实例

publicFactoryInstance createFactoryInstance(String id, ConfigMap properties) {

//生成一个Spring的实例

SpringFactoryInstance instance =newSpringFactoryInstance(this, id, properties);

instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));

returninstance;

}

publicObject lookup(FactoryInstance inst)

{

SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;

returnfactoryInstance.lookup();

}

//内部静态类

staticclassSpringFactoryInstanceextendsFactoryInstance

{

//内部类构造函数

SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)

{

super(factory, id, properties);

}

//用于测试

publicString toString()

{

return"SpringFactory instance for id="+ getId() +" source="+ getSource() +" scope="+ getScope();

}

//查询

publicObject lookup()

{

//这就是从spring容器中getbean了

ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());

String beanName = getSource();

try

{

returnappContext.getBean(beanName);

}

catch(NoSuchBeanDefinitionException nexc)

{

ServiceException e =newServiceException();

String msg ="Spring service named '"+ beanName +"' does not exist.";

e.setMessage(msg);

e.setRootCause(nexc);

e.setDetails(msg);

e.setCode("Server.Processing");

throwe;

}

catch(BeansException bexc)

{

ServiceException e =newServiceException();

String msg ="Unable to create Spring service named '"+ beanName +"' ";

e.setMessage(msg);

e.setRootCause(bexc);

e.setDetails(msg);

e.setCode("Server.Processing");

throwe;

}

}

}

(2)在Spring配置文件中注册相应flex实现接口(跟正常配置spring一样)

(3)在Services-config.xml中加入如下代码:

指定flex加载bean的工厂类的路径

(4)最后,在remoting-config.xml中加入如下代码:

spring//Flex工厂类

clienToServerManager//Flex接口bean

参考文档:《整合Flex和Java—配置篇》

《基于Cairngorm的Flex应用程序设计》

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐