Spring Web Flow entry demo (3) nested process and business integration with source code, flowdemo

Source: Internet
Author: User

Spring Web Flow entry demo (3) nested process and business integration with source code, flowdemo


In the previous blog, we mentioned that Spring web Flow can be combined with businesses in three ways. Next we will introduce the third application method.

 

3. Execute the <action-state> element.

In SpringWeb Flow, this <action-state> is a state designed to execute business logic. If the business logic code of an application is neither suitable for being triggered by the client in the transition nor the custom entry point of Spring Web Flow, therefore, you can add the <action-state> element to be used to execute the business logic. It is more inclined to trigger an event for execution.

Example of action-state:

<action-state id="addToCart"><evaluate expression="cart.addItem(productService.getProduct(productId))"/><transition to="productAdded"/></action-state>

Add a subflow Node

 

The product list has been implemented. The next step is:

 

  1. Implement two business classes: Cart and CartItem
  2. Add configuration in shopping. xml
  3. Add addToCart. xml under/WEB-INF/flows directory
  4. Add the location of addToCart. xml in the webflow-config.xml
  5. Modify viewCart. jsp page

 

Demo implementation:

 

Cart:


Package samples. webflow; import java. io. serializable; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; // implementation class of the shopping Cart public class Cart implements Serializable {private static final long serialVersionUID = 7901330827203016310L; private Map <Integer, CartItem> map = new HashMap <Integer, cartItem> (); // getItems is used to obtain the public List of items in the current shopping cart <CartItem> getItems () {retu Rn new ArrayList <CartItem> (map. values ();} // addItem is used to add the item public void addItem (Product product) {int id = product to the shopping cart. getId (); CartItem item = map. get (id); if (item! = Null) item. increaseQuantity (); else map. put (id, new CartItem (product, 1);} // getTotalPrice is used to get the total price of all items in the shopping cart. public int getTotalPrice () {int total = 0; for (CartItem item: map. values () total + = item. getProduct (). getPrice () * item. getQuantity (); return total ;}}

Cart is the implementation class of the shopping Cart, which also needs to implement java. io. serializable interface, but it does not become Bean managed by Spring IoC container like ProductService. each customer's shopping cart is different, so the default Singleton mode of Spring IoC container cannot be used.

 

CartItem:


Package samples. webflow; import java. io. serializable; // The public class CartItem implements Serializable {private static final long serialVersionUID = 8388627124326126637L; private Product product; // private int quantity; // Number of public CartItem (Product product, int quantity) {this. product = product; this. quantity = quantity;} // calculate the total price of the Entry public int getTotalPrice () {return this. quantity * this. product. getPrice () ;}// increase the number of items public void increaseQuantity () {this. quantity ++;}/*** Return property product */public Product getProduct () {return product;}/*** Sets property product */public void setProduct (Product product) {this. product = product;}/*** Return property quantity */public int getQuantity () {return quantity;}/*** Sets property quantity */public void setQuantity (int quantity) {this. quantity = quantity;}/* getter setter */}

Shopping. xml:


<? 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"> <! -- A Cart object must be allocated at the beginning of shopping flow. To call subflow, this Cart object should be stored in conversationScope. At the same time, you need to add a subflow-state to execute the task of adding a commodity to the shopping cart. --> <! -- Mycart is a service class --> <var name = "mycart" class = "samples. webflow. cart "/> <on-start> <set name =" conversationScope. cart "value =" mycart "> </set> </on-start> <! -- View in view-state corresponds to the jsp page in the jsp folder. on is the trigger event, to corresponds to the state id --> <view-state id = "viewCart" view = "viewCart"> <! -- After entering the render process of the view, before the view actually render is displayed --> <on-render> <! -- To display items on the viewCart page, you only need to call the getProducts method of productService In the on-render entry point of the view-state element, save the result to viewScope. --> <evaluate expression = "productService. getProducts () "result =" viewScope. products "/> </on-render> <transition on =" submit "to =" viewOrder "/> <transition on =" addToCart "to =" addProductToCart "/> </ view-state> <subflow-state id = "addProductToCart" subflow = "addToCart"> <transition on = "productAdded" to = "viewCart"/> </subflow-state> <view-state id = "viewOrder" view = "viewOrder"> <transition on = "confirm" to = "orderConfirmed"> </transition> </view-state> <view- state id = "orderConfirmed" view = "orderConfirmed"> <transition on = "returnToIndex" to = "returnToIndex"> </transition> </view-state> <end-state id = "returnToIndex" view = "externalRedirect: servletRelative:/index. jsp "> </end-state> </flow>

Add addToCart. xml under/WEB-INF/flows directory

The subflow attribute of the subflow-state element indicates that the id of the called flow is "addToCart". Now we need to add the definition of addToCart flow.

AddToCart. xml:

<? 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"> <! -- Before flow execution, the productId field content is obtained from the viewCart page --> <on-start> <set name = "requestScope. productId "value =" requestParameters. productId "/> </on-start> <! -- AddToCart flow is mainly composed of an action-state to add items to the shopping cart. The addToCart flow must have an input parameter, namely, productId. In this example, request parameters are passed and requestParameters is used to obtain the value. Note that the end-state id is "productAdded", which corresponds to the name of the on Attribute of the transition element in subflow-state. --> <Action-state id = "addToCart"> <evaluate expression = "cart. addItem (productService. getProduct (productId) "/> <transition to =" productAdded "/> </action-state> <end-state id =" productAdded "/> </flow>

Location where addToCart. xml is added to the webflow-config.xml


<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring- Context-2.5.xsd "> <! -- Search for the @ Component annotation in the samples. webflow package and deploy it to the container --> <context: component-scan base-package = "samples. webflow"/> <! -- Enable annotation-based configuration --> <context: annotation-config/> <import resource = "webmvc-config.xml"/> <import resource = "webflow-config.xml"/> </beans>

ViewCart. jsp:


<?xml version="1.0" encoding="utf-8" ?><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

ViewOrder. jsp:

<?xml version="1.0" encoding="utf-8" ?><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


Access address:

Http: /localhost: 8080/CartApp5/spring/index

 

Display Effect:



Expand the following:


If we change the configuration file in shopping. xml to flowScope, we can also obtain products data on the viewOrder page.

<View-state id = "viewCart" view = "viewCart"> <! -- After entering the render process of the view, before the view actually render is displayed --> <on-render> <! -- To display items on the viewCart page, you only need to call the getProducts method of productService In the on-render entry point of the view-state element, save the result to viewScope. --> <evaluate expression = "productService. getProducts () "result =" flowScope. products "/> </on-render> <transition on =" submit "to =" viewOrder "/> <transition on =" addToCart "to =" addProductToCart "/> </ view-state>

ViewOrder. jsp:



:



Summary:


The Spring Web Flow application process solves the problem of data access scope, while solving the problem of data access scope, by using xml to control the order of data transfer between pages and data transmission between pages, the jump between pages becomes more flexible and controllable.


Source code




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.