首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Spring Web Flow 2.0 入门(8)

Spring Web Flow 2.0 入门(8)

业务逻辑代码在调用后得到的数据如何保存、传递?Spring Web Flow 的定义中可直接使用表达式语言( Expression Language ),前面的代码都是用的 Unified EL ,对于习惯用 OGNL 的开发人员,可通过 flow-builder-services 的配置改成使用 OGNL 。不管是哪一种表达式语言, Spring Web Flow 都提供了一些固定名称的变量,用于数据的保存、传递。在  一节中,已经提到 Spring Web Flow 所着力解决的问题即是数据存取范围的问题,为此, Spring Web Flow 提供了两种比较重要的范围,一是 flow 范围,另一个是 conversation 范围。通过 flowScope 和 conversationScope 这两个变量, Spring Web Flow 提供了在这两种范围里存取数据的方法。清单 24演示了如何将业务逻辑代码执行的结果存放到flow范围中。
清单 24 flowScope 示例
1
<evaluate expression="productService.getProducts()" result="flowScope.products" />




注意Spring Web Flow 2.0 在默认配置下,flowScope 和 conversationScope 的实现依赖于 Java 序列化和反序列化技术,因此存放于 flowScope 或 conversationScope 中的对象需要实现 java.io.Serializable 接口。

Spring Web Flow 还提供了大量其他的变量,以方便数据的存取。如 viewScope 范围即是从进入 view-state 至退出 view-state 结束, requestScope 即和一般的 request 范围没什么区别,等等。另外还有一些用于获取 flow 以外数据的变量,如 requestParameters 、 messageContext 等等。具体变量的列表可参看 Spring Web Flow自带的文档。
为示例应用添加商品接下来,我们要在示例应用的 viewCart.jsp 页面中添加商品,可按以下步骤操作:
  • 添加 Product 类
  • 添加 ProductService 类
  • 修改 shopping.xml 文件
  • 修改 viewCart.jsp 页面
添加 Product 类Product 类是个普通的 JavaBean ,用于定义商品( Product )的一般属性,同时也提供了构造方法。由于会把 Product 存放于 conversationScope 中, Product 实现了 Serializable 接口。具体见清单25:
清单 25 Product 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package samples.webflow;

import java.io.Serializable;

public class Product implements Serializable {

    private static final long serialVersionUID = 1951520003958305899L;
    private int id;
    private String description;
    private int price;
     
    public Product(int id, String description, int price) {
        this.id = id;
        this.description = description;
        this.price = price;
    }

    /*省略getter和setter*/

}




添加 ProductService 类ProductService 主要提供商品列表,并能根据商品的 id 查找出该商品,由于示例较简单,这里只添加了三条纪录。见清单 26:
清单 26 ProductService 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package samples.webflow;

/*省略import语句*/

@Service("productService")
public class ProductService {
    /*products 用于存放多个商品 */
    private Map<Integer, Product> products = new HashMap<Integer, Product>();
     
    public ProductService() {
        products.put(1, new Product(1, "Bulldog", 1000));
        products.put(2, new Product(2, "Chihuahua", 1500));
        products.put(3, new Product(3, "Labrador", 2000));
    }
     
    public List<Product> getProducts() {
        return new ArrayList<Product>(products.values());
    }
     
    public Product getProduct(int productId) {
        return products.get(productId);
    }
}




Service 注解表示 Spring IoC 容器会初始化一个名为 productService 的 Bean ,这个 Bean 可在 Spring Web Flow 的定义中直接访问。
修改 shopping.xml 文件要在 viewCart 页面中显示商品,只需在 view-state 元素的 on-render 切入点调用 productService 的 getProducts 方法,并将所得结果保存到 viewScope 中即可。见清单27:
清单 27 shopping.xml 需修改的部分
1
2
3
4
5
6
<view-state id="viewCart" view="viewCart" >
  <on-render>
    <evaluate expression="productService.getProducts()" result="viewScope.products"/>
  </on-render>
  <transition on="submit" to="viewOrder"> </transition>
</view-state>




修改 viewCart.jsp 页面清单 27 表明 productService 的 getProducts 方法所得的结果会存放在 viewScope 中名为 products 的变量中, jsp 页面的代码可直接访问该变量。见清单 28:
清单 28 修改后的 viewCart.jsp 页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View Cart</title>
</head>
<body>
<h1>View Cart</h1>
<h2>Items in Your Cart</h2>
<a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
<h2>Products for Your Choice</h2>
<table>
<c:forEach var="product" items="${products}">
<tr>
<td>${product.description}</td>
<td>${product.price}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

返回列表