关于Flex的整合问题,做一个配置简介,另外对容易出错的地方跟大家说说
如果有错误,感谢大家指正。
(开始之前,我必须承认,题目是个噱头,只要有Spring这个超强粘合剂,多个框架可以很容易整合。)
如果是熟悉Spring MVC则完全不需要使用Struts2,这里为了先前项目的表现层可以平滑过度到Flex,才沿用了Struts2
另外,这里主要讲讲Flex怎么与Spring整合,至于Spring与其他框架整合,不在文章内
1.假定你已经配置好web应用并且增加了BlazeDS和导入spring-flex包(使用到的包会在文章最后展示)
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>flexweb</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- The filter for struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Http Flex Session attribute and binding listener support --> <!-- <listener> <listener-class>flex.messaging.HttpFlexSession</listener-class> </listener> --> <!-- MessageBroker Servlet 单独为Flex配置xml--> <servlet> <servlet-name>flex</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/flex-application-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- The filter mapping for struts2 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Map all /messagbroker requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>flex</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
以上内容实质上是两个Web Framework,一个是Struts2(标记名为struts2),一个是Spring MVC(标记名为flex)
这里Spring MVC为flex的RemotingObject提供映射和Flex与Server通讯MessageBroker类
配置flex-application-config.xml
<?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-2.5.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"> <flex:message-broker/>
大家注意到,标签引入http://www.springframework.org/schema/flex/spring-flex-1.0.xsd文件,便可以使用<flex:message-broker/> 标记了。
但是这样写虽然简便,但在初学的时候,还是最好理解它的机制吧
以上的内容可以改写成:
<?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-2.5.xsd <bean id="_messageBroker" class="org.springframework.flex.core.MessageBrokerFactoryBean"> <property name="servicesConfigPath"> <value>/WEB-INF/flex/services-config.xml</value> </property> </bean> <!-- Maps request paths at /* to the BlazeDS MessageBroker --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value>/*=_messageBroker</value> </property> </bean> <!-- Dispatches requests mapped to a MessageBroker --> <bean class="org.springframework.flex.servlet.MessageBrokerHandlerAdapter" />
大家注意:
“http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">内容被移除”
完全是一个标准的Spring配置文件了。
这里把之前web.xml的/messagebroker/*映射完全对应到/*=_messageBroker,也就是
org.springframework.flex.core.MessageBrokerFactoryBean类中,生成MessageBroker,而MessageBroker是Flex与Server通信的关键
而MessageBrokerHandlerAdapter适配器会取得MessageBroker的实例,使用endpoint = broker.getEndpoint(endpointPath, contextPath);取得端点
endpoint.service(request, response);发送
如果你熟悉Spring MVC,请原谅我又唠叨一遍- -#
现在通讯问题完毕,那么远程调用呢?
我们接着来:
定义一个java类:
package example.fx; public class DataBean { private String name = ""; private String params = ""; public String getName() { return this.getClass().getName()+ " : " +name; } public void setName(String name) { this.name = name; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } }
在flex-application-config.xml中,增加:
当然,你也可以写到Spring的其他配置文件中,例如本文中Spring管理Struts2的那个applicationContext.xml(参照web.xml)
<bean id="dataBean" class="example.fx.DataBean"> <property name="params"> <value>Hello Flex!!!</value> </property> </bean>
这里只生成了一dataBean的实例,但是前段Flex如何访问呢?
在在flex-application-config.xml中继续增加
<flex:remoting-destination ref="dataBean" />
这里又是图开发速度,然而这部分可以有如下三种写法,分别是:
a.
<bean id="dataBean" class="example.fx.DataBean"> <property name="params"> <value>Hello Flex!!!</value> </property> <flex:remoting-destination /> </bean>
b.
<flex:remoting-destination ref="dataBean" include-methods="read, update" exclude-methods="create, delete" channels="my-amf, my-secure-amf" />
c.
<bean id="product" class="org.springframework.flex.remoting.RemotingDestinationExporter"> <property name="messageBroker" ref="_messageBroker" /> <property name="service" ref="dataBean" /> <property name="destinationId" value="dataBean" /> <property name="includeMethods" value="read, update" /> <property name="excludeMethods" value="create, delete" /> <property name="channels" value="my-amf, my-secure-amf" /> </bean>
当然,dataBean中并没有read, update, create, delete方法,这里只是演示用.
经过这么多繁琐的过程,终于Flex可以访问到Server的java类了。
我们新建一个MXML Application文件
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; private function onResultHandler(event:ResultEvent):void{ Alert.show(String(event.result), String(example.data)); } private function onFaultHandler(event:FaultEvent):void{ Alert.show(String(event.fault), "Fault!"); } private function btn_Click1EventHandler(event:MouseEvent):void{ dbRemote.getName(); } private function btn_Click1EventHandler(event:MouseEvent):void{ dbRemote.getParams(); } ]]> </mx:Script> <mx:RemoteObject id="dbRemote" destination="dataBean" endpoint="http://localhost:8080/flexweb/messagebroker/amf" result="onResultHandler(event)" fault="onFaultHandler(event)"> </mx:RemoteObject> <mx:Button x="10" id="btn_Click1" label="RemoteClass->getName" click="btn_Click1EventHandler(event)"/> <mx:Button x="80" id="btn_Click2" label="RemoteClass->getParams" click="btn_Click2EventHandler(event)"/> </mx:Application>
服务器启动后,运行你的MXML Application,点击Flash上的两个按钮,看看结果吧
如果你跟我一样懒
那么配置一下Struts,在URL敲一下地址,不用每次都运行你的MXML文件,特别是为了安全起见,你把MXML文件编译到web/WEB-INF/下的时候
以下是struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" namespace="" extends="struts-default"> <action name="*" class="com.opensymphony.xwork2.ActionSupport"> <result name="success">/WEB-INF/page/flex/{1}.swf</result> </action> </package> </struts>
有几点说明:
1.flex的四个配置文件完全没有更改,并且使用默认channel.
2.有文章说,在remoting-service配置远程访问
比如: