要配置 Spring Web Flow 支持 AJAX,可以按照以下步骤进行:
1. 配置视图解析器
在 Spring 配置文件中,需要配置一个 AjaxUrlBasedViewResolver
,并指定其视图类为 FlowAjaxTiles3View
。这将使 Spring Web Flow 能够处理 AJAX 请求并渲染页面片段。
<bean id="tilesViewResolver" class="org.springframework.webflow.mvc.view.AjaxUrlBasedViewResolver">
<property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTiles3View"/>
</bean>
2. 配置 Tiles 视图定义
确保你的 Tiles 配置文件中定义了可以作为片段渲染的部分。例如:
<definition name="hotels/index" extends="standardLayout">
<put-attribute name="body" value="index.body" />
</definition>
<definition name="index.body" template="/WEB-INF/hotels/index.jsp">
<put-attribute name="hotelSearchForm" value="/WEB-INF/hotels/hotelSearchForm.jsp" />
<put-attribute name="bookingsTable" value="/WEB-INF/hotels/bookingsTable.jsp" />
</definition>
3. 在页面中使用 Spring JavaScript 库
Spring JavaScript 提供了一些工具来简化 AJAX 请求的处理。确保在页面中引入 Spring JavaScript 库,并使用其提供的方法来发送 AJAX 请求。例如:
<script type="text/javascript" src="/resources/spring/Spring.js"></script>
<script type="text/javascript" src="/resources/spring/Spring-Dojo.js"></script>
<script type="text/javascript">
function submitSearch() {
var searchCriteria = { /* 收集搜索条件 */ };
Spring.remoting.submitForm('searchForm', 'search', null, null, searchCriteria);
}
</script>
4. 在 Web Flow 中处理 AJAX 请求
在你的 Web Flow 配置中,可以通过 render
元素指定需要渲染的页面片段。例如:
<view-state id="enterSearchCriteria" view="enterSearchCriteria.xhtml">
<on-entry>
<render fragments="hotelSearchForm" />
</on-entry>
<transition on="search" to="reviewHotels">
<evaluate expression="searchService.search(searchCriteria)" result="flowScope.searchResults"/>
</transition>
</view-state>
5. 启用嵌入式模式(可选)
如果你希望在页面中嵌入流并使用 AJAX 进行部分更新,可以在启动流时传递 mode=embedded
参数。例如:
<a href="/hotels/booking?mode=embedded">Start Booking</a>
6. 配置 Flow-Registry
确保你的 flow-registry
配置正确,以便 Spring Web Flow 能够找到你的流程定义文件。例如:
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF/views">
<webflow:flow-location-pattern value="/**/*-flow.xml" />
</webflow:flow-registry>
通过以上配置,Spring Web Flow 将能够支持 AJAX 请求,并允许你进行部分页面更新和动态交互。
以上内容已对 Spring Web Flow 1.0 RC4 版本发布及 Ajax 支持 进行了清晰解读,若需进一步补充或调整,可随时告知。
如需以更结构化的形式呈现,可参考以下排版:
Spring Web Flow 1.0 RC4 版本关键信息
1. Spring Web Flow 模块定位
- 属于 Spring 框架 的子模块,专注于 流程驱动的 Web 应用开发。
- 典型应用场景:
- 多步骤表单提交(如注册、订单确认流程)。
- 复杂业务工作流(如审批流程、任务状态机)。
2. RC4(Release Candidate 4)的含义
- 候选发布版(Release Candidate, RC) 是正式版发布前的最后阶段版本,功能已基本冻结,主要用于 最终稳定性测试。
- RC4 表示这是该版本系列的第 4 个候选版,已接近正式版(GA, General Availability),适合生产环境预部署验证。
3. Ajax 支持的技术价值
- Ajax(Asynchronous JavaScript and XML) 通过异步通信实现网页局部更新,避免全页面刷新,提升用户体验。
- 在 Spring Web Flow 中的典型应用场景:
场景 Ajax 实现效果 动态流程状态更新 无需刷新页面即可显示当前流程步骤、进度或错误信息。 异步表单提交 提交表单后实时验证数据并返回结果(如校验失败提示)。 流程参数动态加载 根据用户输入动态加载下一步骤的表单字段或选项(如级联下拉菜单)。
4. 版本升级的潜在影响
- 优势:结合流程管理与异步交互,简化复杂流程的前端开发,减少服务器压力。
- 注意事项:
- 需确保前端代码与后端流程定义的 事件机制 匹配(如通过
@EventMapping
处理 Ajax 请求)。 - 可能需要额外配置 Spring MVC 的异步支持 或集成其他 JavaScript 框架(如 jQuery、Vue.js)实现交互逻辑。
- 需确保前端代码与后端流程定义的 事件机制 匹配(如通过
如需进一步探讨技术细节(如配置示例、兼容性说明等),可提供具体方向!
Spring Web Flow 与 AJAX 的结合使用主要体现在以下几个方面:
1. Spring Web Flow 的 AJAX 支持
Spring Web Flow 2.0 及以上版本提供了对 AJAX 的原生支持。这意味着在处理 AJAX 请求时,Web Flow 的事件处理机制与 AJAX 事件处理相一致。在处理完成后,可以刷新客户端的相关界面代码。
2. 配置 AJAX 支持
为了支持 AJAX 请求,需要在 Spring Web Flow 的配置中进行一些设置。例如,可以使用 AjaxUrlBasedViewResolver
来处理 AJAX 请求,并创建 FlowAjaxTilesView
对象来渲染响应片段。
<bean id="tilesViewResolver" class="org.springframework.webflow.mvc.view.AjaxUrlBasedViewResolver">
<property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTiles3View"/>
</bean>
3. 处理 AJAX 请求
在 Spring Web Flow 中,可以通过 render
元素来指定需要渲染的页面片段。例如,如果要将 hotelSearchForm
片段转换为一个弹出窗口,可以在 view-state
中定义如下:
<view-state id="changeSearchCriteria" view="enterSearchCriteria.xhtml" popup="true">
<on-entry>
<render fragments="hotelSearchForm" />
</on-entry>
<transition on="search" to="reviewHotels">
<evaluate expression="searchCriteria.resetPage()"/>
</transition>
</view-state>
4. 客户端 AJAX 请求
客户端可以通过 Spring JavaScript 库来发送 AJAX 请求。Spring JavaScript 提供了一些工具来简化 AJAX 请求的处理,并且可以与 Spring Web Flow 的事件处理机制无缝集成。
5. 示例
以下是一个简单的示例,展示如何在 Spring Web Flow 中使用 AJAX:
启动流程时设置嵌入模式
<a href="/hotels/booking?mode=embedded">Start Booking</a>
在页面中使用 AJAX 请求
function submitSearch() {
var searchCriteria = { /* 收集搜索条件 */ };
$.ajax({
url: '/hotels/booking',
type: 'POST',
data: searchCriteria,
success: function(response) {
// 更新页面的特定部分
$('#searchResults').html(response);
}
});
}
在 Spring Web Flow 中定义视图状态
<view-state id="enterSearchCriteria" view="enterSearchCriteria.xhtml">
<transition on="search" to="reviewHotels">
<evaluate expression="searchService.search(searchCriteria)" result="flowScope.searchResults"/>
</transition>
</view-state>
通过以上配置和代码,Spring Web Flow 可以与 AJAX 无缝结合,实现动态的页面更新和交互。
We are pleased to announce that Spring Web Flow 1.0 RC4 has been released.
Download | Documentation | Changelog | Upgrade Guide
Spring Web Flow is a product of the Spring community focused on the orchestration of user interface flow within a web application.
This release contains many improvements and several exciting new features. We consider it the most stable release to-date and, at last, the release that makes the Spring Web Flow 1.0 final roadmap feature complete. Spring Web Flow 1.0 final will be released next week with minimal changes. Between now and then we encourage you to test 1.0 RC4 to help catch any remaining issues before the big 1.0 launch.
Note there are user-affecting changes in this release. 1.0 RC3 or earlier users should review the upgrade guide which outlines these changes in detail.
The new and noteworthy in 1.0 RC4 is an exciting list, including:
New and Noteworthy
As the final release candidate before Spring Web Flow 1.0 final, Spring Web Flow 1.0 RC4 introduces powerful new features such as render actions (1), evaluate actions (2), set actions (3), flash scope (4), flow execution attributes (5), and always redirect on pause (6). It provides enhanced documentation, better flow definition validation, smart defaults, and a complete custom Spring 2.0 configuration schema (7) for configuring the flow execution engine.
(1) Render actions execute application behavior before a response is rendered. A render action is invoked when a view-state is asked to make a renderable view selection, on entry or on a refresh triggered by a redirect or brower refresh button. The following example shows a render-action that executes a search of the phonebook before the results view is rendered.
<view-state id="displayResults" view="searchResults">
<render-actions>
<bean-action bean="phonebook" method="search">
<method-arguments>
<argument expression="flowScope.searchCriteria"/>
</method-arguments>
<method-result name="results"/>
</bean-action>
</render-actions>
<transition on="newSearch" to="enterCriteria"/>
<transition on="select" to="browseDetails"/>
</view-state>
(2) Evaluate actions evaluate expressions against flow execution state. The expression (OGNL-based by default) can be against any object reachable from the flow execution’s root RequestContext, including objects in any scope such as flow scope. The following example shows an evaluate-action that invokes the "makeGuess" method on the "game" flow-scoped bean:
<action-state id="makeGuess">
<evaluate-action expression="flowScope.game.makeGuess(requestParameters.guess)">
<evaluation-result name="guessResult"/>
</evaluate-action>
<transition on="CORRECT" to="showAnswer"/>
<transition on="*" to="enterGuess"/>
<transition on-exception="java.lang.NumberFormatException" to="enterGuess"/>
</action-state>
(3) Set actions set attribute values in scope types such as flow scope. The attribute may be a top-level attribute or a property at a nested property path. The following example shows a set-action that sets the "fileUploaded" attribute to "true" in flash scope.
<action-state id="uploadFile">
<action bean="uploadAction" method="uploadFile"/>
<transition on="success" to="selectFile">
<set attribute="fileUploaded" scope="flash" value="true"/>
</transition>
</action-state>
(4) Flash scope is a new scope type for persisting attributes across a redirect and any refreshes of the view. When an event is signaled to transition out of the view flash scope is cleared. The following complete flow definition example shows use of flash scope to expose a "fileUploaded" attribute to the selectFile view-state for displaying a success message after a successful upload.
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">
<start-state idref="selectFile"/>
<view-state id="selectFile" view="fileForm">
<transition on="submit" to="uploadFile"/>
</view-state>
<action-state id="uploadFile">
<action bean="uploadAction" method="uploadFile"/>
<transition on="success" to="selectFile">
<set attribute="fileUploaded" scope="flash" value="true"/>
</transition>
</action-state>
</flow>
(5) Flow execution attributes allow you to set custom attributes that can influence flow execution behavior. The following example shows an instruction to set the "alwaysRedirectOnPause" attribute to false in a Portlet environment (where redirecting doesn’t tend to apply).
<flow:executor id="flowExecutor" registry-ref="flowRegistry">
<flow:execution-attributes>
<flow:alwaysRedirectOnPause value="false"/>
</flow:execution-attributes>
</flow:executor>
(6) "Always redirect on pauses" gives you default POST+REDIRECT+GET behavior with no special coding. Now by default, when a view state is entered a redirect is issued automatically. This triggers a refresh to a flow execution URL that remains stable while the conversation is active.
(7) The new Spring 2.0 Configuration Dialect greatly simplifies system configuration and provides strong validation and tools support. Configuring webflow’s infrastructure is now as simple as defining two elements, as shown in a complete manner below:
<?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.0.xsd">
<!– Launches new flow executions and resumes existing executions. –>
<flow:executor id="flowExecutor" registry-ref="flowRegistry"/>
<!– Creates the registry of flow definitions for this application –>
<flow:registry id="flowRegistry">
<flow:location path="/WEB-INF/flows/**-flow.xml"/>
</flow:registry>
</beans>
See the reference manual for more information on these features. Spring Web Flow 1.0 RC4 further refines the reference documentation, providing 70 pages on SWF usage. The manual is available on-line in HTML and PDF forms.
Getting Started
One of the best ways to get started with Spring Web Flow is to review and walkthrough the sample applications. We recommend reviewing all samples, supplementing with reference manual material as needed from the start. Ten sample applications ship with the release, each demonstratinga distinct set of product features. These samples are:
Phonebook - the original sample demonstrating most features (including subflows)
Sellitem - demonstrates a wizard with conditional transitions, flow execution redirects, custom text field formatting, and continuations
Flowlauncher - demonstrates all the possible ways to launch and resume flows
Itemlist - demonstrates REST-style URLs and inline flows
Shippingrate - demonstrates Spring Web Flow together with Ajax technology
NumberGuess - demonstrates stateful beans, evaluate actions, and "single key" flow execution redirects.
Birthdate - demonstrates Struts integration
Fileupload - demonstrates multipart file upload, set actions, and flash scope
Phonebook-Portlet - the phonebook sample in a Portlet environment (notice how the flow definitions do not change)
Sellitem-JSF - the sellitem sample in a JSF environment
To build the sample applications for quick evaluation simply:
Extract the spring-webflow-1.0-rc4.zip release archive
Access the projects/spring-webflow/build-spring-webflow directory
Execute the "ant dist" target.
See the "target/artifacts" directory for deployable .war files for each sample as well as expanded war directories.
See the release readme.txt and projects/spring-webflow/spring-webflow-samples/readme.txt for more information on the release archive contents and samples, respectively.
All sample projects are Spring IDE projects directly importable into Eclipse.
Thanks to everyone out there who supported this release. Spring Web Flow 1.0 is now… finally… just around the corner.