You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Name

Spring Webflow Plugin

Author

Tom Schneider

Homepage

http://code.google.com/p/struts2webflow/

Version

1.0.0-beta1

Compatibility

Struts 2.0.2+

State

Unstable

License

Open Source (ASL2)

Download

http://code.google.com/p/struts2webflow/downloads/list

Overview

This plugin integrates Spring Webflow (SWF) with Struts2. It allows struts2 to execute spring webflows and allows spring webflows to have struts2 actions as webflow actions. There is also a sample car insurance app that illustrates usage of the plugin.

Features

  • Allows struts2 to execute spring webflows
  • Allows struts2 actions to be invoked by spring webflow.
  • Includes an interceptor that injects flow scope properties into struts2 actions.
  • Includes an annotation-based interceptor that injects flow scope properties into struts2 actions.
  • SWF flow execution key can be managed in the session rather than as a hidden field on the client.

Requirements

  • Spring 2.0+
  • Spring Webflow 1.0+
  • Struts 2.0.2+
  • Struts2Webflow 1.0.0+

Installation

This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib directory.

Getting Started

Prerequisite

For background information on the core Spring Webflow concepts, visit http://www.ervacon.com/products/swf/intro/.

Step 1 - Create your flow definition xml

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
                          http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

	<start-state idref="ageEnter" />

	<view-state id="ageEnter" view="ageEnter">
		<transition on="submit" to="AgeSave" />
	</view-state>

	<view-state id="ageEnterJSP" view="ageEnter">
		<transition on="input" to="enterAgeJSP" />
		<transition on="submit" to="AgeSave" />
	</view-state>

	<action-state id="AgeSave">
		<action bean="struts2FlowAdapter"/>
		<transition on="input" to="enterAgeJSP" />
		<transition on="success" to="calcRate" />
	</action-state>

	<view-state id="calcRate" view="calcRate">
		<transition on="finish" to="finish" />
	</view-state>

	<end-state id="finish" view="finish"/>
</flow>

Step 2 - Configure Spring's applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:flow="http://www.springframework.org/schema/webflow-config"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/webflow-config
           http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd">

	<!-- Launches new flow executions and resumes existing executions. -->	
	<flow:executor id="flowExecutor" registry-ref="flowRegistry">
		<flow:execution-attributes>
		  <flow:alwaysRedirectOnPause value="false"/>
		</flow:execution-attributes>
	</flow:executor>
	
	<!-- Creates the registry of flow definitions for this application -->
	<flow:registry id="flowRegistry">
		<flow:location path="/WEB-INF/flows/**-flow.xml"/>
	</flow:registry>
	
	<bean id="struts2FlowAdapter" class="com.googlecode.struts2webflow.Struts2FlowAdapter"></bean>
</beans>

The Struts2FlowAdapter allows a struts2 action to execute a webflow action-state. The Struts2FlowAdapter uses the id of the action state as the name of struts2 action to execute. The alwaysRedirectOnPause is disabled because whether to redirect or not is easier to control in the struts2 configuration files.

Step 3 - Add SWF interceptors to struts2's struts.xml configuration file

<interceptors>
	<interceptor name="sessionFlowExecKey" class="com.googlecode.struts2webflow.SessionFlowExecKeyInterceptor"/>
	<interceptor name="annotationflowScope" class="com.googlecode.struts2webflow.annotations.AnnotationFlowScopeInterceptor"/>
</interceptors>

The SessionFlowExecKeyInterceptor puts the flow execution key in the session rather than having it as a hidden field on the form that submitted back. The AnnotationFlowScopeInterceptor uses annotations to bind struts2 action variables to and from flow scope. Before an action executes, this interceptor looks for @FlowIn annotated properties of the struts2 action and populates the these actions from flow scope. After the action has executed, properties annotated with the @FlowOut annotation are put back into flow scope.

Step 4 - Configure the FlowAction so Spring Webflows can be executed

<action name="FlowAction" class="com.googlecode.struts2webflow.FlowAction">
	<interceptor-ref name="sessionFlowExecKey" />
	<interceptor-ref name="defaultStack" />
	<param name="flowId">rating-flow</param>
	<result name="ageEnter" type="redirect">
		AgeEnter.action
	</result>
	<result name="ageEnterJSP">/example/enterage.jsp</result>
	<result name="calcRate" type="redirect">
		CalcRate.action
	</result>
	<result name="finish">/example/finished.jsp</result>
</action>

For each view-state defined in the flow xml definition, there should be a corresponding result entry in the FlowAction definition. (Or a global result for that view-state)

Step 5- Access the flow

The flow can now be launched by accessing the FlowAction.

Version History

Version

Date

Author

Notes

1.0.0-beta1

Jan 4, 2007

Tom Schneider

Initial release

  • No labels