Flex+Struts2+Spring整合开发

关于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>

[color=red]当然,dataBean中并没有read, update, create, delete方法,这里只是演示用[/color].
经过这么多繁琐的过程,终于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文件,[color=blue]特别是为了安全起见,你把MXML文件编译到web/WEB-INF/下的时候[/color]
以下是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配置远程访问
比如:

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">

<adapters>
<adapter-definition id="java-object"
class="flex.messaging.services.remoting.adapters.JavaAdapter"
default="true" />
</adapters>

<default-channels>
<channel ref="my-amf" />
</default-channels>
<!-- 远程调用 -->
<destination id="dataBean">
<properties>
<source>example.fx.DataBean</source>
</properties>
</destination>
</service>

这种方式和在文章中的flex-application-config.xml配置的
[color=blue]<flex:remoting-destination ref="dataBean" /> [/color]是一致的,也就是说,如果你在remoting-service配置了,就不需要在Spring配置文件中,反之亦然,如果你在remoting-service和flex-application-config.xml都配置了,web服务器启动的时候会抛异常,提示你bean id已经注册.
3.有不少文章中,MXML中RomoteObject是这样配置的

<mx:AMFChannel id="myamf" uri="http://localhost:8080/flexweb/messagebroker/amf"/>
<mx:ChannelSet id="channelSet" channels="{[myamf]}"/>
<mx:RemoteObject id="dBean"
destination="dataBean" channelSet="{channelSet}" result="onResultHandler(event)" fault="onFaultHandler(event)"/>

如果使用默认配置,即remoting-service.xml文件中会有一个默认channel配置,这样写是完全没有必要的,你只需要在
<mx:RemoteObject id="dbRemote" destination="dataBean" endpoint="http://localhost:8080/flexweb/messagebroker/amf"/> 
定义一个server的端点
上面的写法这相当于你另外写了一个AMFChannel,并且使用管道去接(ChannelSet里可以放很多channel)

jar包:
Spring 使用Spring 3.0.0.M2
Struts 使用Struts 2.0.4
Flex 使用默认BlazeDS包和org.springframework.flex-1.0.0.RC2.jar包
稍后设置一下下载地址
----------------------------------------------------------------
BlazeDS:
[url]http://opensource.adobe.com/wiki/display/blazeds/Release+Builds[/url]
org.springframework.flex-1.0.0.RC2.jar:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值