TechnoBuzz

A Techno Blog, mainly about Java

Spring Web Flow Beginner

As part my learnings of what is new with the Spring Framework, I found a nice Spring Flow 2.0 Example (with JSF and JPA).

The Spring Web Flow 2.0.2 release ( by Erwin Vervaet and Keith Donald ) comes as a separate download from the basic Spring functionality in 2.5.5.

On the Spring Web Flow (SWF) vision from 1.x to 2.x :

“[in 1.0] the SWF controller engine cared for one half of the web request lifecycle; the half related to request processing, often called the action phase. The other half, the render phase, was pushed off on the caller: either Spring MVC, Struts, or JSF front controllers
…..
The downside of this approach is it makes it difficult to apply application control logic during the view rendering phase
…..
Beginning with Web Flow 2.0 M2, the entire web request lifecycle is now under the control of Spring Web Flow, including the view rendering phase.
….
the ability for the SWF engine to communicate with external systems and conversational contexts over HTTP (embedding the proper flow execution callback URL in the redirect that is sent to the external system)”

Reference Guide 2.x

Also, note that getting these Spring Flow 2.x reference sample projects are easy to get a hold of. In the download of Web Flow is a projects/build-spring-webflow directory where you can run ant. It will build Web Flow along with the .war files for the sample projects (ant 1.7 and Java 5 are required to build).

There is a Struts2  plugin for Spring Web Flow called  struts2webflow

As far as further examples than the ones above, I found some web flow 2.x examples at this site spring by example.

Spring flow 1.0 showed that it could  easily integrate with struts. These  sample (1.x) applications can also be found on Java Passion Site. A good 1.x reference is the  Practical ntroduction . All the samples projects are Spring IDE projects that importable into Eclipse (see springide or plugin central for spring ide).

Here in the 1.x struts example:

Our first action in the jsp is as follows:

<A href=”flowAction.do?_flowId=birthdate”>Birth Date</A>

This action in the struts config says:

<action path=”/flowAction” name=”actionForm” scope=”request” type=”org.springframework.webflow.executor.struts.FlowAction”/>

We also bind to the Action Form:

<form-bean name=”actionForm” type=”org.springframework.web.struts.SpringBindingActionForm”/>

In the webflow-config.xml we define the flow registry:
<flow:executor id=”flowExecutor” registry-ref=”flowRegistry”/>
<!– Creates the registry of flow definitions for this application –>
<flow:registry id=”flowRegistry”>
<flow:location path=”/WEB-INF/birthdate.xml”/>
<flow:location path=”/WEB-INF/birthdate-alternate.xml”/>
</flow:registry>

Start State : the first state in the flow

<start-state idref=”enterBirthdate” />

View State: selects a view to render

<view-state id=”enterBirthdate” view=”birthdateForm”>

When the execution of the flow starts, enter the enterBirthdate state. Then select the birthdateForm view for display to the user, and pause the flow of execution until a user event happens.

Render Action: initializes the form object.

<render-actions>
<action bean=”formAction” method=”setupForm” />
</render-actions>

For view state, above Initializes the backing “form object” by invoking the setupForm method for formAction.

Note that the action was defined in the webflow-config.xml (instance of spring-webflow-config-1.0.xsd)

<bean id=”formAction” class=”org.springframework.webflow.samples.birthdate.BirthDateFormAction” />

Transition: Each View state must define a transition that leads to another state

<transition on=”submit” to=”processBirthdateFormSubmit” />

Action state: logic that needs to be executed in context of the request, once executed the result flow is returned which the flow may respond to.

<action-state id=”processBirthdateFormSubmit”>
<action bean=”formAction” method=”bindAndValidate”>
<attribute name=”validatorMethod” value=”validateBirthdateForm” />
</action>

transition on=”success” to=”enterCardInformation” />
<transition on=”error” to=”enterBirthdate” />

</action-state>

Related Links for Basic info on JSF:

Advertisements

July 13, 2008 Posted by | J2EE | 21 Comments