Saturday, July 16, 2011

Spring Web Flow - Web flow states and transitions in simple terms

The following example takes you through the following states in a web flow.


view-state
end-state
action-state
decision-state

1. If you are using Maven to build your project, you need to include the web flow dependencies to your project as defined in Spring Web flow dependencies from maven

2. Every flow begins with the following schema definition.


<?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-2.0.xsd">

</flow>

3. Use view-state to render a page during the flow.


<?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-2.0.xsd">


<!-- The first state defined in the flow becomes the initial state -->
<!--
Use view state if you want to render a view The view attribute is
resolved using the view resolvers
-->
<view-state id="addUser" view="AddUser"></view-state>
</flow>

4. Transition element is used to handle events that occurs within a state.

<?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-2.0.xsd">


<!-- The first state defined in the flow becomes the initial state -->
<!--
Use view state if you want to render a view The view attribute is
resolved using the view resolvers
-->
<view-state id="addUser" view="AddUser">
<transition on="Next" to="AddressDetails"></transition>
  <transition on="Cancel" to="CancelAdd"></transition>
</view-state>


<!--
If the view attribute is not specified, spring web flow tries to
resolve the id to the view
-->
<view-state id="AddressDetails">
<!--
The events are triggered in view state when the user clicks the
button or link on the web page
-->
<transition on="Next" to="EducationalDetails"></transition>
<transition on="Cancel" to="CancelAdd"></transition>
</view-state>
</flow>

5. Use end-state element to complete a web flow.


<?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-2.0.xsd">


<end-state id="CancelAdd">
</end-state>


<end-state id="CreateUser">
</end-state>
</flow>

No comments:

Post a Comment