領先一步
VMware 提供培訓和認證,以加速您的進度。
了解更多我最近完成了 Spring Web Flow 中一個有趣的問題。這個問題 (SWF-163) 處理的是為 Spring Web Flow 的內部 scopes 增加 Spring 2.0 bean scoping 支援。 實作本身並不是那麼有趣 (畢竟 Scope 介面非常容易實作),但我想要說明如何在您的應用程式中使用類似的東西。
使用最新的 Spring Web Flow 1.1 快照,我們現在看到三個主要的 Web Flow scopes 的 bean scopes,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 scopes,您需要利用 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 貢獻的任何三個 scopes。
<?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 的配置的主要變更在於,現在 variable resolver 是來自 Spring 而不是 Spring Web Flow。 當 JSP 頁面尋找 sale 變數時,JSF 將委派給 Spring 進行 bean 解析,並且 bean 實例將根據其定義上的 scope 屬性進行 scoped。
如果您想使用此新功能,它將很快隨 Spring Web Flow 1.1-m1 版本一起推出,或者您可以透過下載最新的 Spring Web Flow 1.1-m1 nightly snapshot 取得預覽。