Flex与Web应用程序整合技术分享
Flex与Web应用程序整合技术分享
发表于1年前(2012-07-10 17:17)?? 阅读(117)?|?评论(
0)
3人收藏此文章, 我要收藏
赞0
flex
spring
web
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 ? ? ? ? ? ? ? ?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的配置(直接拷贝即可)。 <context-param> <param-name>flex.class.path</param-name> <param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value> </context-param> <!-- Http Flex Session attribute and binding listener support --> <listener> <listener-class>flex.messaging.HttpFlexSession</listener-class> </listener> <!-- MessageBroker Servlet --> <servlet> <servlet-name>MessageBrokerServlet</servlet-name> <display-name>MessageBrokerServlet</display-name> <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class> <init-param> <param-name>services.configuration.file</param-name> <param-value>/WEB-INF/flex/services-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MessageBrokerServlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> 以上步骤需在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 n???????Event将会被映射到Command n???????Command可能被映射到Delegate n???????Delegate映射到Service n???????Command将使用Service传回的结果更新Model Locator 3.???Flex和Spring整合 ????????????(1)?????????首先,需要有一个加载bean的工厂类,我的项目中为SpringFactory.java,类中的代码固定publicclass SpringFactory implements FlexFactory{ ??? ???? ??? ?//定义一个常量资源 ??? ?privatestaticfinal String SOURCE = "source";?? ??? ? ??? ? ??? ?publicvoid initialize(String id,ConfigMap configMap) { ??? ?? ??? ?}? ??? ?/** ??? ????? *Thismethodiscalledwhenweinitializethedefinitionofaninstance ??? ????? *whichwillbelookedupbythisfactory.? Itshouldvalidatethat ??? ????? *thepropertiessuppliedarevalidtodefineaninstance. ??? ????? *Anyvalidpropertiesusedforthisconfigurationmustbeaccessedto ??? ????? *avoidwarningsaboutunusedconfigurationelements.? Ifyourfactory ??? ????? *isonlyusedforapplicationscopedcomponents,thismethodcansimply ??? ????? *returnafactoryinstancewhichdelegatesthecreationofthecomponent ??? ????? *totheFactoryInstance'slookupmethod. ??? ????? */ ??? ?//创建一个factory实例 ??? ?public FactoryInstance createFactoryInstance(String id,ConfigMap properties) { ??? ?//生成一个Spring的实例 ??? ?SpringFactoryInstance instance = new SpringFactoryInstance(this,id,properties); ??? ? ??? ????? ??? ???????instance.setSource(properties.getPropertyAsString(SOURCE,instance.getId()));?? ??? ??????? return instance;?? ??? ?} ? ??? ? ??? ?public Object lookup(FactoryInstance inst)?? ??? ??? {?? ??? ??????? SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;?? ??? ??????? return factoryInstance.lookup();?? ??? ??? }??? ??? ? ??? ?//内部静态类 ??? ?staticclass SpringFactoryInstance extends FactoryInstance?? ??? ??? {?? ??? ?//内部类构造函数 ??? ??????? SpringFactoryInstance(SpringFactory factory,String id,ConfigMap properties)?? ??? ??????? {?? ??? ??????????? super(factory,properties);?? ??? ??????? } ?? ??? ?????? //用于测试 ??? ??????? public String toString()?? ??? ??????? {?? ??? ??????????? return"SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();?? ??? ??????? }?? ??? ??????? //查询 ??? ??????? public Object lookup()??? ??? ???????{?? ??? ???????? //这就是从spring容器中getbean了 ??? ??????????? ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());?? ??? ??????????? String beanName = getSource(); ?? ??? ? ??? ??????????? try? ??? ??????????? {?? ??? ??????????????? return appContext.getBean(beanName);?? ??? ??????????? }?? ??? ??????????? catch (NoSuchBeanDefinitionException nexc)?? ??? ??????????? {?? ??? ??????????????? ServiceException e = new ServiceException();?? ??? ??????????????? String msg = "Spring service named '" + beanName + "' does not exist.";?? ??? ??????????????? e.setMessage(msg);?? ??? ??????????????? e.setRootCause(nexc);?? ??? ??????????????? e.setDetails(msg);?? ??? ??????????????? e.setCode("Server.Processing");?? ??? ??????????????? throw e;?? ??? ??????????? }?? ??? ??????????? catch (BeansException bexc)?? ??? ??????????? {?? ??? ??????????????? ServiceException e = new ServiceException();?? ??? ??????????????? String msg = "Unable to create Spring service named '" + beanName + "' ";?? ??? ??????????????? e.setMessage(msg);?? ??? ??????????????? e.setRootCause(bexc);?? ??? ??????????????? e.setDetails(msg);?? ??? ??????????????? e.setCode("Server.Processing");?? ??? ??????????????? throw e;?? ??? ??????????? }??? ??? ??????? } ??? ? ??? ??? } (2)?????????在Spring配置文件中注册相应flex实现接口(跟正常配置spring一样) ???????????????(3)?????????在Services-config.xml中加入如下代码: ? <factories> <factory id="spring"class="flexHandle.SpringFactory"/> </factories> 指定flex加载bean的工厂类的路径 ????????(4)?????????最后,在remoting-config.xml中加入如下代码: <destination id="Client2Server"> ??????? ??? <properties> ??????? ??? ??? <factory>spring</factory>//Flex工厂类 ??????????? <source>clienToServerManager</source>//Flex接口bean ??????? ??? </properties> ?</destination> ? ? 参考文档:《整合Flex和Java—配置篇》 ????????????? ?《基于Cairngorm的Flex应用程序设计》 参考网站:http://blog.dreamhui.net/archives/64 ?????????http://www.iteye.com/topic/180613 ?????????http://www.oschina.net/question/12_7654 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |