Spring Webflow Code example - Part IV

For previous parts:
For Part-I click on: Spring Webflow Code example - Part I

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

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

Lets create the third page which is to provide a complementary drinks, if the user has selected 'Indian Spicy Pizza'. Now since this page needs to be shown conditionally, we need to make use of 'action-state' element. Using this, we can evaluate a condition and then make the transition accordingly.

In the view-state for 'selectPizza', insert the transition for 'next' to a state 'complementaryDrinks'. This will actually be a action state as shown below.

Note the action state 'complementaryDrinks' will invoke a method hasComplementaryDrinks() to determine the transition.

Modify the pizza-order-flow.xml as below:
<view-state id="selectPizza" model="viewScope.newpizza">
        <on-render>
              <evaluate expression="pizzaOrderController.createPizza()" result="viewScope.newpizza"/>
        </on-render>
        <transition on="previous" to="customerDetails" />
        <transition on="add" to="selectPizza">                 
              <evaluate expression="pizzaOrderController.addPizza(flowScope.order,viewScope.newpizza)"/>
        </transition>
        <transition on="next" to="complementaryDrinks" />
        <transition on="clear" to="selectPizza">
              <evaluate expression="pizzaOrderController.clearPizzas(flowScope.order)"/>
        </transition>
        <transition on="cancel" to="cancelOrder" />
</view-state>
 
<action-state id="complementaryDrinks">
    <evaluate expression="pizzaOrderController.hasComplementaryDrinks(flowScope.order)" />
    <transition on="yes" to="showDrink" />
    <transition on="no" to="reviewOrder" />
</action-state>

<view-state id="showDrink">
     <transition on="previous" to="selectPizza" />
     <transition on="next" to="reviewOrder" />
     <transition on="cancel" to="cancelOrder" />
</view-state>

Controller changes:

Add new method hasComplementaryDrinks() which will check if the user has selected 'Indian Spicy Pizza'.

public String hasComplementaryDrinks(Order order){
         String outcome = "no";
         for(Pizza pizza: order.getPizzaList()){
             if("Indian Spicy Pizza".equals(pizza.getName())){
                   outcome = "yes";
                   break;
             }
         }
         return outcome;
}

New JSP for complementary message:

<form:form modelAttribute="pizza" action="${flowExecutionUrl}">
<h1><spring:message code="complimentMsg"/></h1>

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


Review Order page:

In this page, we will show the details from the first two pages for review. Clicking on 'submit' will submit the order for processing and provide an order Id back.

Modify 'pizza-order-flow.xml' to add the following view state.

<view-state id="reviewOrder" model="order">   
        <transition on="previous" to="selectPizza" />
        <transition on="submit" to="orderConfirmation" >
               <evaluate expression="pizzaOrderController.submitOrder(flowScope.order)" />
        </transition>
        <transition on="cancel" to="cancelOrder" />
</view-state>

The code sample for the controller and jsp for the next section are ommitted as they are pretty straight forward.
Create a method in the controller 'submitOrder' which will invoke a service to process the orde and update the order object with order number. 

Similarly create JSP page 'reviewOrder.jsp' and display the customer details and pizza selected along with the navigation buttons.




End states:

The end states define the state points at which the flow will terminate. In our example, we have two end states, one when the user has submitted the 'Review Order' page and one when the user has clicked on 'Cancel'. 

<end-state id="orderConfirmation"  view="orderConfirmation"/>
<end-state id="cancelOrder"  view="cancelOrder"/>


Create a JSP 'orderConfirmation.jsp' which will show the order number. 

This completes the webflow tutorial. I hope the example was complete enough to provide a simple flow up and running.

Comments

  1. Could you share source code?

    ReplyDelete
    Replies
    1. You can download the source code from:
      https://docs.google.com/file/d/0B79rs4rdp74TSFdUeEZiaWpMMEU/edit?usp=sharing

      Delete
  2. Easy steps to explain the web flow. Thanks

    ReplyDelete
  3. Its very good blog for spring-webflow begginers

    ReplyDelete
  4. This is great example, excellent work. Thanks for sharing.

    ReplyDelete
  5. how to down load this source code

    ReplyDelete
  6. i got 404 error please let me know how to solve this

    ReplyDelete
  7. Hi Suresh,
    Can you let me know at which step are you facing issue? Were you able to download the source code?

    ReplyDelete

Post a Comment

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