Spring Web Flow Bean 作用域和 JSF

工程 | Ben Hale | 2007年5月8日 | ...

我最近完成了 Spring Web Flow 中的一個有趣問題。這個問題(SWF-163)涉及到為 Spring Web Flow 的內部作用域新增 Spring 2.0 bean 作用域支援。實現本身並不是那麼有趣(畢竟 Scope 介面相當容易實現),但我想提一下你如何在你的應用程式中使用類似這樣的東西。

Spring 2.0 作用域

在 Spring 1.x 中,我們有單例和原型 bean 作用域的概念,但表示法是固定的,並且用 singleton="[true | false]" 來描述並不是特別清晰。因此在 Spring 2.0 中,這種表示法從 XSD 樣式的配置中移除,現在您看到的是更清晰的表示法 scope="[singleton | prototype | ...]"。Spring 本身增加了三個更多的 bean 作用域;requestsessionglobalSession,它們與 Web 應用程式相關。

透過 Spring Web Flow 1.1 的最新快照,我們現在看到了三個主要的 Web Flow 作用域的 bean 作用域:flashflowconversation


<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 提供的任何三個範圍。

作用域 bean 和 JSF

目前,這些新範圍最引人注目的用途是在 JSF 中。透過在標準的 Spring bean 定義檔案中使用 scope 約定而不是在流定義中使用 <var/> 約定,JSF 使用者體驗會更接近標準的 JSF 行為。要為 JSF 新增此功能,您需要註冊標準的 Spring JSF DelegatingVariableResolver。其他定義(FlowNavigationHandlerFlowPhaseListener)是使用 Spring 和 JSF 的標準配置。

<?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 屬性進行範圍限定。

結論

這樣做的最終結果是,JSF 使用者現在可以使用類似於內建樣式的語法,但在一個功能更強大的容器中。

如果您想使用這個新功能,它將在 Spring Web Flow 1.1-m1 版本中很快釋出,或者您可以透過下載最新的 Spring Web Flow 1.1-m1 夜間快照來獲得預覽。

獲取 Spring 新聞通訊

透過 Spring 新聞通訊保持聯絡

訂閱

領先一步

VMware 提供培訓和認證,助您加速進步。

瞭解更多

獲得支援

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案,只需一份簡單的訂閱。

瞭解更多

即將舉行的活動

檢視 Spring 社群所有即將舉行的活動。

檢視所有