on the previous blog we said Spring Web Flow There are three main ways to integrate with the business, below we mainly introduce the third kind of application method
3 , execute to <action-state> element
This <action-state> in Springweb Flow is the state that is designed to execute business logic. If the business logic code for an app is neither suitable for the client to trigger in transition, nor is it suitable for the Spring Web Flow custom pointcut, then consider adding the <action-state> element dedicated to the execution of that business logic. More inclined to trigger an event to execute.
action-state Example:
<action-state id= "AddToCart" ><evaluate expression= "Cart.additem (productservice.getproduct (productId))"/ ><transition to= "productadded"/></action-state>
Adding Subflow Nodes
The list of items has been implemented and the following steps are:
- Implementation of Cart and Cartitem two business classes
- adding configurations in Shopping.xml
- Add Addtocart.xml under the/web-inf/flows directory
- Add Addtocart.xml location in Webflow-config.xml
- Modify Viewcart.jsp Page
specific 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;//Shopping Cart implementation class public class Cart implements Serializable { private static final long Serialversionuid = 7901330827203016310L; Private Map<integer, cartitem> Map = new Hashmap<integer, cartitem> (); GetItems is used to get items in the current shopping cart public list<cartitem> GetItems () {return new arraylist<cartitem> (map.values ()); } AddItem used to add goods to the shopping cart public void AddItem (product product) {int id = Product.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 ()) T Otal + = Item.getproduct (). GetPrice () * item.getquantity (); return total; }}
The cart is the implementation class of the shopping cart, which also implements the Java.io.Serializable interface, but it does not become a Bean that is managed by the Spring IoC container like Productservice, and each customer's shopping cart is different and therefore cannot use Spring The IoC container default Singleton mode.
Cartitem:
package samples.webflow;import java.io.serializable;//items in shopping cart public class Cartitem Implements Serializable {private static final long serialversionuid = 8388627124326126637L; Private product product;//Commodity private int quantity;//number public Cartitem (product product, int quantity) {This.product = Product;this.quantity = Quantity; }//Calculates 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 "><!--at the beginning of shopping flow must be assigned a Cart object, this Cart object should be stored in conversationscope due to the call to Subflow. Also add a subflow-state to perform the task of adding items to the shopping cart. --><!--Mycart as a service class--><var name= "Mycart" class= "Samples.webflow.Cart"/><on-start><set Name= "Conversationscope.cart" value= "Mycart" ></set></on-start><!-- The view in view-state corresponds to the JSP page in the JSP folder, on is the trigger event, to the state ID--><view-state id= "viewcart" view= "Viewcart" ><!-- After entering the render flow of the view,--><on-render><!--to display the item in the Viewcart page before the view actually render it, just the on-rende of the view-state element The R pointcut invokes Productservice's GetProducts method and saves the resulting result in Viewscope to--><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 the/web-inf/flows directory
The Subflow attribute of the Subflow-state element indicates that the called flow has an ID of "AddToCart", and it is now necessary 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 "> <!--flow execution, ProductId This field content gets--><on-start><set name= "Requestscope.productid" value= "from the Viewcart page" Requestparameters.productid "/></on-start><!--AddToCart flow is mainly composed of a action-state, to complete the function of adding items to the shopping cart, The implementation of AddToCart flow requires an input parameter, or productId. In this example, a request parameter is passed to get the value by Requestparameters. It is also important to note that the ID of the end-state is "productadded", corresponding to the name of the on property 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>
Where to add Addtocart.xml in 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 @Component annotations in the Samples.webflow package and deploy them 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 it again:
If we change the configuration file in Shopping.xml to Flowscope, we can also get the products data on the Vieworder page.
<view-state id= "Viewcart" view= "Viewcart" ><!--after entering the render flow of the view, before the view actually render out--><ON-RENDER&G t;<!--to display the item in the Viewcart page, simply call Productservice's GetProducts method in the On-render pointcut of the view-state element and save the resulting result in Viewscope- -><evaluate expression= "productservice.getproducts ()" result= "Flowscope.products"/></on-render> <transition on= "Submit" to= "ViewOrder"/><transition on= "AddToCart" to= "Addproducttocart"/></ View-state>
vieworder.jsp:
:
Summarize:
Spring Web Flow application process to solve the problem of data access scope, and in the solution of data access scope problems, through the use of XML to control the flow between pages and data transfer between pages, so that the jump between our pages become more flexible and controllable.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring Web Flow Starter Demo (iii) nested process and business integration source