Spring Webflow Code example - Part III

For Part-I click on: Spring Webflow Code example - Part I
For Part-II click on: Spring Webflow Code example - Part II

Let's create the second page 'Select Pizza'. In this page, we will show a drop down of available pizzas. This page will have buttons 'Add', 'clear' in addition to navigation buttons 'Next', 'Previous', 'Cancel'.
Once the user has selected a pizza, the user can click on 'Add' button to add the pizza to the user's cart. The user can use the 'Clear' button to clear the cart, if he wants to start all over again.

Controller changes:

In order to initialize the collection for this drop down, we will create a new method 'initPizzaList' in the controller. We also need a method to initialize the Pizza object which will be the model for the jsp.

Further, we need a controller method 'addPizza' to update the Order model with the pizzas added. We also need a 'clearPizzas' method as well.

public List initPizzaList(){
                List pizzaList = new ArrayList();
                pizzaList.add("NY Cheese Pizza");
                pizzaList.add("Indian Spicy Pizza");
                pizzaList.add("Italian Special Pizza");
                return pizzaList;
public Pizza createPizza(){
               return  new Pizza();
public void addPizza(Order order,Pizza pizza){
public void clearPizzas(Order order){

Changes to pizza-order-flow.xml:

<view-state id="selectPizza" model="viewScope.newpizza">
              <evaluate expression="pizzaOrderController.createPizza()" result="viewScope.newpizza"/>
        <transition on="previous" to="customerDetails" />
        <transition on="add" to="selectPizza">                 
              <evaluate expression="pizzaOrderController.addPizza(flowScope.order,viewScope.newpizza)"/>
        <transition on="clear" to="selectPizza">
              <evaluate expression="pizzaOrderController.clearPizzas(flowScope.order)"/>
        <transition on="cancel" to="cancelOrder" />
If you observe the above view state, the model object is defined with view scope. This is because, the Pizza object will need to exist only till life span of the view and once user clicks on 'Add' this Pizza object will be added to the Order objects pizzaList.

The addPizza() method, will accept two parameters, first is the Order object from the flowScope and second is the Pizza object from the view scope. 

So, from the third page onwards, the view Scope 'Pizza' object will not be available and only the 'Order' object from the flow Scope will be available.

New JSP 'selectPizza.jsp'

Create a new jsp as below:
<form:form modelAttribute="newpizza" action="${flowExecutionUrl}">
<table width="40%"  border="0" cellspacing="3" cellpadding="1">
<td><spring:message code="selectPizza"/></td>
<td><form:select path="name" items="${pizzaList}"/></td>
<td><spring:message code="quantity"/></td>
<td><form:input path="quantity"/></td>

<table width="20%"  border="0" cellspacing="3" cellpadding="1">
<button type="submit" id="add" name="_eventId_add">
        <spring:message code="add"/>
<button type="submit" id="clear" name="_eventId_clear">
        <spring:message code="clear"/>

<table width="40%" border="1" cellspacing="3" cellpadding="1">

<tr><th><spring:message code="pizzaName"/></th><th><spring:message code="quantity"/></th></tr>
<c:forEach items="${order.pizzaList}" var="pizza">

<table width="30%"  border="0" cellspacing="3" cellpadding="1">
<button type="submit" id="previous" name="_eventId_previous">
       <spring:message code="previous"/>
<button type="submit" id="next" name="_eventId_next">
       <spring:message code="next"/>
<button type="submit" id="cancel" name="_eventId_cancel">
       <spring:message code="cancel"/>


                                                                                               ....Click here to continue


Popular posts from this blog

Pivotal Cloud Foundry (PCF) Integration with Elastic Cloud Storage (ECS)

Restful code example using Spring MVC

Spring Integration - Bulk processing Example