首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

flex与java透过BlazeDS实现交互(整合spring)

2012-10-07 
flex与java通过BlazeDS实现交互(整合spring)BlazeDS指的是flex程序中通过RemoteObject方式(amf)进行数据传

flex与java通过BlazeDS实现交互(整合spring)

BlazeDS指的是flex程序中通过RemoteObject方式(amf)进行数据传输的一系列控件与服务器端程序(例如java)中一系列第三方jar包一起组成的数据交互框架。通过这种方式,我们可以直接将java中数据模型直接映射到flex程序中,省略了交互过程中解析对象的步骤。

一.客户端配置:


?? 1.使用flex builder实现方式

????? 在flex builder中服务器构建方式中选择j2ee的BlazeDS,指定服务器端services-config.xml所在的目录,根url路径和上下文访问路径。

编译完成后flex的编译参数里会附加services-config.xml路径地址。这种方式需要客户端和服务器端放在一台服务器上,客户端程序会先去读取服务上的services-config.xml配置文件,无法解决分布式部署的问题,也增加了程序的耦合性,所以我个人不推荐这种方式。

?? 2.不耦合IDE的实现方式:

(1)定义AMF通道,需要跟services-config.xml中的一致(要注意的是如果url写成localhost,其他用户访问时会有沙箱冲突的问题)

?

public static const _AmfChannel : AMFChannel = new AMFChannel("amf","http://192.168.1.200:80/messagebroker/amf"); ?

(2)往amfChannelSet中添加AmfChannel,通过注册了amfChannelSet的RemoteObject获取AbstractOperation ,

添加事件监听,然后发送

?

var remoteService:RemoteObject=new RemoteObject(“serverName”);   var amfChannelSet : ChannelSet = new ChannelSet;  amfChannelSet.addChannel(_AmfChannel);  remoteService.channelSet = amfChannelSet;   var op : AbstractOperation = remoteService.getOperation(“functionName”);  op.addEventListener(ResultEvent.RESULT,doCallBack);  op.addEventListener(FaultEvent.FAULT,faultCallBack);  op.send(“parameters”); 
?

?

通过这种方式无需验证services-config.xml,但是否发送成功仍需取决于services-config.xml中配置的channel和destination

二.服务器端配置(以整合spring3.0为例)

spring和flex的整合是spring项目组下顶级子项目之一,但由于中文资料较少,网上流传的方法大多没有使用到org.springframework.flex.jar。两种方式都需要spring3.0和BlazeDS的几个jar包,在这就不累述了。

以下是两种实现方式:

1.不使用org.springframework.flex.jar的方式

(1)在web.xml中配置servlet

?

<servlet> <servlet-name>MessageBrokerServlet</servlet-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> ?

(2)在services-config.xml注册数据通道

<channels> <channel-definition id="my-amf" /> </channel-definition> <channels> ?

(3)在services-config.xml中配置factory

springFactory需要继承FlexFactory,重写lookup方法,在lookup方法里通过srping的ApplicationContext去查找对应的bean以加载spring容器。

?

<factories> <factory id="springFactory" /> </factories> ??

(4)services-config.xml中加载remoting-config.xml

<services> <service-include file-path="remoting-config.xml" /> </services> ?

remoting-config.xml文件

在这里serviceName既是你前台访问的serviceName,beanName既是spring容器里beanName

你可以重写flex.messaging.services.remoting.adapters.JavaAdapter以实现中央控制器的功能

这样你就可以通过前台传入serviceName访问到对应的bean,通过functionName访问到对应的方法

<service id="remoting-service"              default="true" />      </adapters>      <default-channels>          <channel ref="my-amf" />      </default-channels>      <destination id="serviceName">          <properties>              <factory>springFactory</factory>              <source>beanName</source>          </properties>      </destination>  </service>
?

2.使用org.springframework.flex.jar的方式

(1)配置web.xml

载入spring配置文件

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:conf/spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> //spring mvc的servlet <servlet> <servlet-name>MessageBrokerServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:conf/spring/flex-servlet.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> ?

(2)配置flex-servlet.xml

这是一个spring mvc中的sevlet,其中指明了services-config.xml路径

javaAdapter既是上文中的中央控制器

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"> <flex:message-broker services-config-path="classpath*:conf/flex/services-config.xml"> </flex:message-broker> <bean id="javaAdapter" /> </bean> </beans> ?

(3)services-config.xml与上文中基本一样,不过由于已经载入spirng的环境,所以不需要springFactory了,也不需要引入remoting-config.xml,因为所有的destination定义都可通过下文的注解方式实现。

(4)这里使用spring3.0中的注解方式实现,前台可以通过serviceName访问到exampleService

@Service("serviceName") @RemotingDestination(channels={"my-amf"},serviceAdapter="javaAdapter") public class exampleService{ } ?

3.使用ide查看定义的destination

(1) 在不使用spring提供的jar情况下,由于前台flex需要读取后台的remoting-config.xml配置文件,所以ide例如flex builder能通过查看数据源看到remoting-config.xml中定义的destination,但仍需在web.xml中添加如下配置。

<servlet> <servlet-name>RDSDispatchServlet</servlet-name> <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class> <init-param> <param-name>useAppserverSecurity</param-name> <param-value>true</param-value> </init-param> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping id="RDS_DISPATCH_MAPPING"> <servlet-name>RDSDispatchServlet</servlet-name> <url-pattern>/CFIDE/main/ide.cfm</url-pattern> </servlet-mapping> ?

(2)在使用spring提供的jar情况下,由于destination动态存在于spring的容器中,前台无法直接获取,所以无法查看到destination

热点排行