領先一步
VMware 提供培訓和認證,助您加速進步。
瞭解更多我最近完成了 Spring Web Flow 中的一個有趣問題。這個問題(SWF-163)涉及到為 Spring Web Flow 的內部作用域新增 Spring 2.0 bean 作用域支援。實現本身並不是那麼有趣(畢竟 Scope 介面相當容易實現),但我想提一下你如何在你的應用程式中使用類似這樣的東西。
透過 Spring Web Flow 1.1 的最新快照,我們現在看到了三個主要的 Web Flow 作用域的 bean 作用域:flash、flow 和 conversation。
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="flash"/>
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="flow"/>
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="conversation"/>
要利用這些 bean 範圍,您需要使用新的 1.1 版本配置(包含在 Web Flow jar 中)併為您的 bean 定義新增一個單獨的元素。
<?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:flow="http://www.springframework.org/schema/webflow-config"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.1.xsd">
<flow:enable-scopes/>
<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale" scope="conversation"/>
</beans>
給定應用程式上下文中只需要存在一次 <enable-scopes/> 標籤,它將允許您使用 Spring Web Flow 提供的任何三個範圍。
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<application>
<navigation-handler>
org.springframework.webflow.executor.jsf.FlowNavigationHandler
</navigation-handler>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
<lifecycle>
<phase-listener>
org.springframework.webflow.executor.jsf.FlowPhaseListener
</phase-listener>
</lifecycle>
</faces-config>
與之前的 Web Flow 啟用配置相比,主要的改變是現在變數解析器是來自 Spring 而不是 Spring Web Flow。當 JSP 頁面查詢 sale 變數時,JSF 會將 bean 解析委託給 Spring,並且 bean 例項將根據其定義上的 scope 屬性進行範圍限定。
如果您想使用這個新功能,它將在 Spring Web Flow 1.1-m1 版本中很快釋出,或者您可以透過下載最新的 Spring Web Flow 1.1-m1 夜間快照來獲得預覽。