Friday, September 13, 2013

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){
                order.addPizza(pizza);
}
 
public void clearPizzas(Order order){
                order.clearPizzas();
}

Changes to pizza-order-flow.xml:

<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="clear" to="selectPizza">
              <evaluate expression="pizzaOrderController.clearPizzas(flowScope.order)"/>
        </transition>
        <transition on="cancel" to="cancelOrder" />
</view-state>
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">
 
<tr>
<td><spring:message code="selectPizza"/></td>
<td><form:select path="name" items="${pizzaList}"/></td>
</tr>
 <tr>
<td><spring:message code="quantity"/></td>
<td><form:input path="quantity"/></td>
</tr>
</table>

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

<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">
<tr>
<td>${pizza.name}</td>
<td>${pizza.quantity}</td>
</tr>
</c:forEach>
</table>

<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>



                                                                                               ....Click here to continue

No comments:

Post a Comment