第8章 使用Spring Web Flow--學習一個簡單例子
概述:
Spring 的工作流就是類似網上購物,註冊使用者-》選擇商品-》新增到購物車-》付款-》收貨-》售後,一套已經固化的流程。
學習博文地址:
看一下工程結構:
操作步驟:
step1、引入spring web flow jar 當前還要普通Spring相關jar包
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.4.5.RELEASE</version>
</dependency>
step2、配置執行環境
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>webflow</display-name> <servlet> <servlet-name>webServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>webServlet</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
配置集合Springmvc+flow配置檔案
<?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"> <context:component-scan base-package="com.jack"/> <!-- 啟動配置註解 --> <context:annotation-config/> <import resource="webmvc.xml"/> <import resource="webflow.xml"/> </beans>
配置Springmvc
<?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">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"/>
</bean>
<!-- SimpleUrlHandlerMapping 通過配置檔案,把一個URL對映到Controller -->
<bean id="viewMappings"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/shopping.do=flowController</value>
</property>
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</property>
</bean>
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
</beans>
總結:
1、首先配置jsp通用的檢視解析器viewResolver
2、SimpleUrlHandlerMapping 可以將map與控制器類關聯,格式類似: path = 控制器。例如上 /shopping.do = flowController(flowController 也就是工作流的入口)
3、UrlFilenameViewController功能類似InternalResourceViewResolver,它將URL解釋成檢視例如:"/index.html" -> "index"
4、FlowController類似董事長(BP) 而下屬flowExecutor 類似執行長(CEO),老大不需要自己親自去弄,叫ceo傳達一下就行
配置flow (為執行長配置資源和任務)
<?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"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
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
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
<webflow:flow-executor id="flowExecutor"/>
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
<webflow:flow-location path="classpath:shopping.xml" id="shopping"/>
<webflow:flow-location path="classpath:addToCart.xml" id="addToCart"></webflow:flow-location>
</webflow:flow-registry>
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator"/>
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="viewResolver"/>
</bean>
</beans>
總結:
1、webflow:flow-executor 就是預設執行長
2、分配的任務就是shopping.xml和addToCart.xml
3、工作流中需要渲染工作,這時候服務工作就交給了viewResolver
step3、配置工作流
歡迎頁-》挑選商品-》加入購物-》計算總數量和總價格
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/spring-webflow-2.0.xsd">
<var name="mycart" class="com.jack.Cart"/>
<on-start>
<set name="conversationScope.cart" value="mycart"/>
</on-start>
<view-state id="viewCart" view="viewCart">
<on-render>
<evaluate expression="productService.getProducts()" result="viewScope.products"/>
</on-render>
<transition on="submit" to="viewOrder">
</transition>
<transition on="addToCart" to="addProductToCart"></transition>
</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>
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">
<on-start>
<set name="requestScope.productId" value="requestParameters.productId" />
</on-start>
<action-state id="addToCart">
<evaluate expression="cart.addItem(productService.getProduct(productId))"></evaluate>
<transition to="productAdded"/>
</action-state>
<end-state id="productAdded"/>
</flow>
總結:
1、<var>表示定義變數,Cart就是購物車,就像你去超市購物,在入口推著購物車,每人一個
2、<on-start>表示開始時候準備好,conversationScope.cart (表示作用域,簡單就是在整個一次購物中,這個購物車只會你使用)
3、<view-state>表示jsp顯示的時候,告訴需要顯示的view=“viewCart” 表示為viewCart.jsp
4、<on-render>表示在顯示jsp的時候需要做一些事情,比如裝置一些資料,類似在進商場之前,工作人員已經將貨物上架,不然你買啥
5、<transition>表示接下一個步驟,類似你看到喜歡商品,需要做的一個動作就是將商品放入購物車,這裡有兩種情況,加入購物車還是不加入
6、不加入就去前臺結賬,如果加,這是需要增加加的動作,這裡它增加一個字流程<subflow-state> 注意屬性subflow="addToCart"
7、你會發現addToCart在shopping.xml,它在addToCart.xml, 注意它標籤是<action-state>而不是<view-state> 它是一個動作而不是展示檢視
8、新增完商品轉到了productAdded,也就是shopping.xml這裡來了,發現它又轉到viewCart,它要更新商品數量和型別
9、假如,我們已經新增好了商品,點選submit, 它轉到viewOrder,viewOrder展示你已經選擇的商品
10、點選confirm表示你確認你購買的商品
11、最後重定向到externalRedirect:servletRelative:/index.jsp ,也就是首頁
12、注意addToCart.xml定義requestScope.productId (表示在請求作用域中設定一個屬性為productId ,它的值取自requestParameters.productId[本身請求的過來的資料])
13、xml如何實現java程式碼,使用SpEL ,也就是Spring Expression Language,例如expression="cart.addItem(productService.getProduct(productId))"
總的來說就是它用xml畫了一個流程圖
步驟4、jsp檔案
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello!</h1>
<br>
<a href="shopping.do">ViewCart</a>
</body>
</html>
orderConfirmed.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Order Confirmed</h1>
<br>
<a href="${flowExecutionUrl }&_evenId=returnToIndex">Return to index</a>
</body>
</html>
viewCart.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>View Cart</h1>
<h2>Items in Your Cart</h2>
<c:choose>
<c:when test="${empty cart.items}">
<p>Your cart is empty.</p>
</c:when>
<c:otherwise>
<table border="1" cellspacing="0">
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
<c:forEach var="item" items="${cart.items }">
<tr>
<td>${item.product.descript }</td>
<td>${item.quantity }</td>
<td>${item.product.price }</td>
<td>${item.totalPrice }</td>
</tr>
</c:forEach>
<tr>
<td>Total:</td>
<td></td>
<td></td>
<td>${cart.totalPrice }</td>
</tr>
</table>
</c:otherwise>
</c:choose>
<a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
<h2>Products for Your Choice</h2>
<table>
<c:forEach var="product" items="${products}">
<tr>
<td>${product.descript }</td>
<td>${product.price }</td>
<td><a href="${flowExecutionUrl}&_eventId=addToCart&productId=${product.id}">[add
to cart]</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
viewOrder.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Order</h1>
<c:choose>
<c:when test="${empty cart.items }">
<p>Your cart is empty.</p>
</c:when>
<c:otherwise>
<table border="1" cellspacing="0">
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
<c:forEach var="item" items="${cart.items }">
<tr>
<td>${item.product.descript }</td>
<td>${item.quantity }</td>
<td>${item.product.price }</td>
<td>${item.totalPrice }</td>
</tr>
</c:forEach>
<tr>
<td>TOTAL:</td>
<td></td>
<td></td>
<td>${cart.totalPrice }</td>
</tr>
</table>
</c:otherwise>
</c:choose>
<a href="${flowExecutionUrl}&_eventId=confirm">Confirm</a>
</body>
</html>
總結:
1、flowExecutionUrl跳轉處理控制器
2、_eventId 表示事件Id
java類:
Cart.java
package com.jack;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Cart implements Serializable{
/**
*
*/
private static final long serialVersionUID = 2218144992574949101L;
private Map<Integer, CartItem> map = new HashMap<Integer, CartItem>();
public List<CartItem> getItems(){
return new ArrayList<CartItem>(map.values());
}
//addItem 用於向購物車新增商品、
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));
}
}
public int getTotalPrice(){
int total =0;
for (CartItem item : map.values()){
total += item.getProduct().getPrice() * item.getQuantity();
}
return total;
}
}
CartItem.java
package com.jack;
import java.io.Serializable;
public class CartItem implements Serializable{
/**
*
*/
private static final long serialVersionUID = 7087013311725981757L;
private Product product;//商品
private int quantity; //數量
public CartItem(Product product, int quantity) {
super();
this.product = product;
this.quantity = quantity;
}
//計算該條目的總價格
public int getTotalPrice(){
return this.quantity*this.product.getPrice();
}
//增加商品的數量
public void increaseQuantity(){
this.quantity++;
}
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
public void setProduct(Product product) {
this.product = product;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
Product.java
package com.jack;
import java.io.Serializable;
public class Product implements Serializable{
/**
*
*/
private static final long serialVersionUID = -3547488052308853185L;
private int id;
private String descript;
private int price;
public Product() {
super();
// TODO Auto-generated constructor stub
}
public Product(int id, String descript, int price) {
super();
this.id = id;
this.descript = descript;
this.price = price;
}
public int getId() {
return id;
}
public String getDescript() {
return descript;
}
public int getPrice() {
return price;
}
public void setId(int id) {
this.id = id;
}
public void setDescript(String descript) {
this.descript = descript;
}
public void setPrice(int price) {
this.price = price;
}
}
ProductService.java
package com.jack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
@Service("productService")
public class ProductService {
private Map<Integer, Product> products = new HashMap<Integer,Product>();
public ProductService(){
products.put(1, new Product(1, "Bulldog", 1000));
products.put(2, new Product(1, "Chihuahua", 1000));
products.put(3, new Product(1, "Labrador", 1000));
}
public List<Product> getProducts() {
return new ArrayList<Product>(products.values());
}
public void setProducts(Map<Integer, Product> products) {
this.products = products;
}
public Product getProduct(int productId){
return products.get(productId);
}
}
相關推薦
第8章 使用Spring Web Flow--學習一個簡單例子
概述: Spring 的工作流就是類似網上購物,註冊使用者-》選擇商品-》新增到購物車-》付款-》收貨-》售後,一套已經固化的流程。 學習博文地址: 看一下工程結構: 操作步驟: step1、引入spring web flow jar 當前還要普通Spring相關jar
Atitit 現代資訊檢索 Atitit 重要章節 息檢索建模 檢索評價 第8章 文字分類 Line 210: 第9章 索引和搜尋 第11章 Web檢索 第13章 結構化文字檢索 目錄 L
Atitit 現代資訊檢索 Atitit 重要章節 息檢索建模 檢索評價 第8章 文字分類 Line 210: 第9章 索引和搜尋 第11章 Web檢索 第13章 結構化文字檢索 目錄 Line 1
第五章構建Spring Web應用程序——SpringMVC起步
而是 ack 前端控制器 ota ring ron urn pan 處理器 跟蹤Spring MVC的請求 註:流程: 1)請求離開瀏覽器到達的第一站就是前端控制器,在這裏一個單例的Servlet將請求委托給應用程序的其他組件來執行實際處理。Dispatche
Spring實戰4 第5章 構建Spring Web應用程式
一,構建Spring Web應用程式## 1,Spring MVC起步 1.1跟蹤Spring Mvc請求 在請求離開瀏覽器是,帶有使用者請求的內容資訊 第一站是Spring的DispatcherServlet,在Spring MVC中,它就是前端控制器。它的任務是將請求傳送給Sp
springcloud系列—Stream—第8章-1: Spring Cloud Stream 訊息驅動
參考:http://blog.didispace.com/spring-cloud-starter-dalston-7-2/ 目錄 快速入門 Spring Cloud Stream是一個用來為微服務應用構建訊息驅動能力的框架。它可以基於Spring Boot來建立獨立的、可用於生產的
springcloud系列—Stream—第8章-3: Spring Cloud Stream 訊息驅動(消費組)
使用消費組實現訊息消費的負載均衡 通常在生產環境,我們的每個服務都不會以單節點的方式執行在生產環境,當同一個服務啟動多個例項的時候,這些例項都會繫結到同一個訊息通道的目標主題(Topic)上。 預設情況下,當生產者發出一條訊息到繫結通道上,這條訊息會產生多個副本被每個消費者例項接收和處理,但
利用Python進行資料分析——第8章繪圖及視覺化——學習筆記Python3 5.0.0
matplotlib API 入門 matplotlib API 函式(如plot和close)都位於matplotlib.pyplot模組中,通常的引入方式如下: import matplotlib.pyplot as plt Figure和Subplot matplot
第8章 Shell函數的知識與實踐
-s area als sent mode pan tex true edit shell 函數常見的語法格式function 函數名(){ return n}簡化1function 函數名{ ...}簡化2函數名(){ ...}在Shell函數的
Spring實戰 | 第二部分 Web中的Spring(第五章 構建Spring Web應用程式)
第五章 構建Spring Web應用程式 對映請求到Spring控制器 透明地繫結表單引數 校驗表單提交 一
spring web mvc 的一個簡單例項
1.準備開發環境和執行環境 開發工具:eclipse-jee-neon-2-win32-x86_64 執行環境:apache-tomcat-8.0.33 專案工程環境:Maven Project spring框架及依賴jar包:通過pom.xml配置 在eclipse中建立
《Spring實戰》學習筆記-第八章:使用Spring Web Flow
第四版的第八章內容與第三版基本一致。 本章內容: 建立會話式web應用程式 定義流程狀態和行為 保護web流程 網際網路的一個奇特之處就在於它很容易讓人迷失。有如此多的內容可以檢視和閱讀,而超連結是其強大魔力的核心所在。 有時候,web應用程式需要控
【C語言學習】《C Primer Plus》第8章 字符輸入/輸出和輸入確認
multipl 字符輸入 信號 first while 目的 bcd 問題 img 學習總結 1、緩沖區分為完全緩沖區(fully buffered)I/O和行緩沖區(line-buffered)I/O。對完全緩沖輸入來說,當緩沖區滿的時候會被清空(緩沖區內容發送至
Oracle塗抹oracle學習筆記第8章RMAN說,我能備份
src 關閉 技術分享 啟動與關閉 host .cn 輸入 啟動 target ---恢復內容開始--- 本次測試服務器為172.16.25.33 使用rman連接本地數據庫 rman target / 在rman中執行啟動與關閉的命令與sqlplus相同 在rman中執
《機器學習實戰》第8章書本正文缺少一個函數
copy AR 機器 reg str 代碼 var div mean 在8.4.2 lasso的stageWist函數裏使用到了一個用於標準化的函數regularize,這個函數是需要自定義的,但是書中沒有給出來。 在示例代碼中有,是這樣子的: def regularize
第8章 異常控制流 (exceptional control flow)
calling lds osi multitask ons rec block 調度 nvi # Wed 24 Jan 11:30:36 GMT 2018 第8章 異常控制流 (exceptional control flow) 8.1 exceptions Except
【Linux學習筆記】第8章 Linux shell基礎知識
linux centos shell 通配 8.1shell介紹shell是一個命令解釋器,提供人機交互。支持特定語法。每個用戶都可以有自己特定的shell(bash)。CentOS7默認bash(Bourne Agin Shell)。其他還有zsh、ksh等。 8.2命令歷史命令歷史存放於
(轉)Django學習之 第三章:動態Web頁面基礎
只有一個 即使 typeerror 方法 對象傳遞 power int() 擔心 意圖 上一章我們解釋了怎樣開始一個Django項目和運行Django服務器 當然了,這個站點實際上什麽也沒有做------除了顯示了"It worked"這條信息以外。 這一章我們介紹怎樣使用
機器學習實戰第8章預測數值型數據:回歸
矩陣 向量 from his sca ima 用戶 targe 不可 1.簡單的線性回歸 假定輸入數據存放在矩陣X中,而回歸系數存放在向量W中,則對於給定的數據X1,預測結果將會是 這裏的向量都默認為列向量 現在的問題是手裏有一些x
SpringBoot | 第三十三章:Spring web Servcies整合和使用
前言 最近有個單位內網系統需要對接統一門戶,進行單點登入和待辦事項對接功能。一般上政府系統都會要求做統一登入功能,這個沒啥問題,反正業務系統都是做單點登入的,改下shiro相關類就好了。看了接入方案,做坑爹的是需要業務系統提供一個webService服務,供統一平臺呼叫。對於ws服務,是
201711671223《Java程式設計》第八週學習總結 第8章常用使用類
201711671223《Java程式設計》第八周學習總結 第8章常用使用類 String類 Java專門提供了用來處理字元序列的String類。String類在java.lang包中,由於java.lang包中的類被預設引入,因此程式可以直接使用String類。 Java把String