更进一步
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 将委托给 Spring 进行 bean 解析,并且 bean 实例将根据其定义上的 scope 属性进行作用域限定。
如果您想使用此新功能,它将很快随 Spring Web Flow 1.1-m1 版本一起发布,或者您可以下载最新的 Spring Web Flow 1.1-m1 nightly snapshot 来获得预览。