

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02
Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

转自:http://www.nirvanastudio.org/nicholas/SWFEditor.htm
如果你还不了解Spring Web Flow,请参考Spring Web Flow这篇文章。
如果你已经开始对Spring Web Flow感兴趣了,那么我们开始着手了解如何独自开发SWF应用了。
首先,一个好的开发环境能够节省很多时间,这里我介绍一下如何使用Eclipse + WebFlowEditor开发SWF应用。
安装插件可以通过Help->Software Updates->Find and Install->Search for new festures to install->New Remote Site的办法,添加插件下载页的方式直接通过eclipse下载安装。
Spring IDE: http://springide.org/updatesite/
WebFlowEditor: http://springide.org/updatesite_dev
GET: 通过默认的Eclipse.org update site安装,或者直接从eclipse网站下载安装
图1 - Spring IDE 视图
这个例子来源于Spring Web Flow发行包内的例子 - fileupload,这是一个很简单的例子,流程也很简单。它采用了Spring MVC,最后我尝试着将它转换成Struts MVC。
在WEB-INF下的upload-flow.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE webflow PUBLIC "-//SPRING//DTD WEBFLOW//EN" "http://www.springframework.org/dtd/spring-webflow.dtd"> <webflow id="upload" start-state="selectFile.view"> <view-state id="selectFile.view" view="selectFile.view"> <transition on="submit" to="bindAndValidate"/> </view-state> <action-state id="bindAndValidate"> <action bean="upload.process"/> <transition on="success" to="confirmation.view"/> <transition on="error" to="selectFile.view"/> </action-state> <view-state id="confirmation.view" view="confirmation.view"> <transition on="back" to="selectFile.view"/> </view-state> </webflow> 通过Spring Web Flow Editor生成的流程图如下:
图2- fileupload 流程
在这个应用里,所有的view都放在了WEB-INF/jsp里面,每个标单里面必须有这样一个隐藏域:
<INPUT type="hidden" name="_flowExecutionId" value="<%=request.getAttribute("flowExecutionId") %>">
用于SWF判断流程执行位置。
SelectFile.view ==> WEB-INF/jsp/selectFile.view.jsp,在这个View里面还有一个隐藏域:
<INPUT type="hidden" name="_eventId" value="submit">
这个用于表示事件ID,就是transition的on属性,这样标示就能让spring知道执行哪个transitioin了。
与Struts集成:
Spring提供的例子是与Spring MVC集成的,我尝试将它换成了Struts。
1、在struts-config.xml里面加入Spring Plugin,并且定义SWF入口:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="actionForm" type="org.springframework.web.struts.BindingActionForm"/>
</form-beans>
<global-forwards>
<forward name="confirmation.view" path="/WEB-INF/jsp/confirmation.view.jsp" />
<forward name="selectFile.view" path="/WEB-INF/jsp/selectFile.view.jsp" />
</global-forwards>
<action-mappings>
<action path="/upload"
type="org.springframework.web.flow.struts.FlowAction"
name="actionForm" scope="request"
className="org.springframework.web.flow.struts.FlowActionMapping">
<set-property property="flowId" value="upload" />
</action>
</action-mappings>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>
</struts-config>
关于view的映射需要使用action forwards,典型的做法是定义成全局的(SWF JavaDocs上介绍)。
定义SWF入口是最重要的,如果不定义,SWF是无法启动的。同时要启动一个Web流程,你需要传递一个flowId给控制器,这里FlowAction包含了一个全局的控制器(详细内容查SWF JavaDocs)。upload这个属性值很重要,他是你的webflow的id号,你可能有多个webflow,用它标示你需要启动的那个。当然还有其他的启动办法,另外的例子上有介绍。
3、编写完整的applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="upload" class="org.springframework.web.flow.config.FlowFactoryBean">
<property name="flowBuilder">
<bean class="org.springframework.web.flow.config.XmlFlowBuilder">
<property name="resource" value="/WEB-INF/upload-flow.xml"/>
</bean>
</property>
</bean>
<bean id="upload.process" class="org.springframework.samples.fileupload.web.flow.ProcessUploadAction">
<property name="formObjectName" value="file"/>
<property name="formObjectClass" value="org.springframework.samples.fileupload.web.flow.FileUploadBean"/>
</bean>
</beans>
4、编写web.xml以加载Struts和Spring:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
结论:
由于没有细看SWF提供的例子,birthdate就是一个Struts + Spring的例子,害得我捉摸了半天......
birthday的例子中提供了Spring MVC和Struts的配置的对比,就是没有讲解,也让人看得很郁闷。
SWF与Struts是两个独立的模块,SWF设计者的初衷是实现最大程度的重用,也就是说SWF开发和Struts开发是相对独立的。不仅是Struts,包括Spring MVC和Spring Portlet MVC都是与之独立的。使用Struts仅仅是作为一个启动WebFlow的入口,就像Spring MVC一样,与之不同的是Struts使用struts-config.xml配合applicationContext配置启动WebFlow入口,而Spring MVC只需要直接定义在applicationContext配置里面,因为Spring都是一家子的东西嘛。
从前一篇翻译的文章里可以看到,Action State的实现是继承org.springframework.web.flow.action包里面的类,而不是针对特定的MVC框架,那么他们是如何通讯的呢。那就是Context对象模式(Context Object Pattern),属于J2EE的表现层模式,可以参考J2EE Core Pattern这个书,上面介绍的详细。这个Context对象提供了在一个全局应用中使用的与协议无关的封装后的对象。这样就为了把SWF和MVC框架分开。
作者:
2005-5-28