Spring JS is designed such that an implementation of its API can be built for any of the popular Javascript toolkits. The initial implementation of Spring.js builds on the Dojo toolkit.
Using Spring Javascript in a page requires including the underlying toolkit as normal, the Spring.js
base interface file, and the Spring-(library implementation).js
file for the underlying toolkit. As an example, the following includes obtain the Dojo implementation of Spring.js using the ResourceServlet
:
When using the widget system of an underlying library, typically you must also include some CSS resources to obtain the desired look and feel. For the booking-mvc reference application, Dojo's tundra.css
is included:
11.4. Spring Javascript Decorations
A central concept in Spring Javascript is the notion of applying decorations to existing DOM nodes. This technique is used to progressively enhance a web page such that the page will still be functional in a less capable browser. The addDecoration
method is used to apply decorations.
The following example illustrates enhancing a Spring MVC <form:input>
tag with rich suggestion behavior:
<form:input id="searchString" path="searchString"/>
<script type="text/javascript">
Spring.addDecoration(new Spring.ElementDecoration({
elementId: "searchString",
widgetType: "dijit.form.ValidationTextBox",
widgetAttrs: { promptMessage : "Search hotels by name, address, city, or zip." }}));
</script>
The ElementDecoration
is used to apply rich widget behavior to an existing DOM node. This decoration type does not aim to completely hide the underlying toolkit, so the toolkit's native widget type and attributes are used directly. This approach allows you to use a common decoration model to integrate any widget from the underlying toolkit in a consistent manner. See the booking-mvc
reference application for more examples of applying decorations to do things from suggestions to client-side validation.
When using the ElementDecoration
to apply widgets that have rich validation behavior, a common need is to prevent the form from being submitted to the server until validation passes. This can be done with the ValidateAllDecoration
:
<input type="submit" id="proceed" name="_eventId_proceed" value="Proceed" />
<script type="text/javascript">
Spring.addDecoration(new Spring.ValidateAllDecoration({ elementId:'proceed', event:'onclick' }));
</script>
This decorates the "Proceed" button with a special onclick event handler that fires the client side validators and does not allow the form to submit until they pass successfully.
An AjaxEventDecoration
applies a client-side event listener that fires a remote Ajax request to the server. It also auto-registers a callback function to link in the response:
<a id="prevLink" href="search?searchString=${criteria.searchString}&page=${criteria.page - 1}">Previous</a>
<script type="text/javascript">
Spring.addDecoration(new Spring.AjaxEventDecoration({
elementId: "prevLink",
event: "onclick",
params: { fragments: "body" }
}));
</script>
This decorates the onclick event of the "Previous Results" link with an Ajax call, passing along a special parameter that specifies the fragment to be re-rendered in the response. Note that this link would still be fully functional if Javascript was unavailable in the client. (See the section on Handling Ajax Requests for details on how this request is handled on the server.)
It is also possible to apply more than one decoration to an element. The following example shows a button being decorated with Ajax and validate-all submit suppression:
<input type="submit" id="proceed" name="_eventId_proceed" value="Proceed" />
<script type="text/javascript">
Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'proceed', event:'onclick'}));
Spring.addDecoration(new Spring.AjaxEventDecoration({elementId:'proceed', event:'onclick',formId:'booking', params:{fragments:'messages'}}));
</script>
It is also possible to apply a decoration to multiple elements in a single statement using Dojo's query API. The following example decorates a set of checkbox elements as Dojo Checkbox widgets:
<div id="amenities">
<form:checkbox path="amenities" value="OCEAN_VIEW" label="Ocean View" /></li>
<form:checkbox path="amenities" value="LATE_CHECKOUT" label="Late Checkout" /></li>
<form:checkbox path="amenities" value="MINIBAR" label="Minibar" /></li>
<script type="text/javascript">
dojo.query("#amenities input[type='checkbox']").forEach(function(element) {
Spring.addDecoration(new Spring.ElementDecoration({
elementId: element.id,
widgetType : "dijit.form.CheckBox",
widgetAttrs : { checked : element.checked }
}));
});
</script>
</div>
11.5. Handling Ajax Requests
Spring Javascript's client-side Ajax response handling is built upon the notion of receiving "fragments" back from the server. These fragments are just standard HTML that is meant to replace portions of the existing page. The key piece needed on the server is a way to determine which pieces of a full response need to be pulled out for partial rendering.
In order to be able to render partial fragments of a full response, the full response must be built using a templating technology that allows the use of composition for constructing the response, and for the member parts of the composition to be referenced and rendered individually. Spring Javascript provides some simple Spring MVC extensions that make use of Tiles to achieve this. The same technique could theoretically be used with any templating system supporting composition.
Spring Javascript's Ajax remoting functionality is built upon the notion that the core handling code for an Ajax request should not differ from a standard browser request, thus no special knowledge of an Ajax request is needed directly in the code and the same hanlder can be used for both styles of request.
Providing a Library-Specific AjaxHandler
The key interface for integrating various Ajax libraries with the Ajax-aware behavior of Web Flow (such as not redirecting for a partial page update) is org.springframework.js.AjaxHandler
. A SpringJavascriptAjaxHandler
is configured by default that is able to detect an Ajax request submitted via the Spring JS client-side API and can respond appropriately in the case where a redirect is required. In order to integrate a different Ajax library (be it a pure JavaScript library, or a higher-level abstraction such as an Ajax-capable JSF component library), a custom AjaxHandler
can be injected into the FlowHandlerAdapter
or FlowController
.
Handling Ajax Requests with Spring MVC Controllers
In order to handle Ajax requests with Spring MVC controllers, all that is needed is the configuration of the provided Spring MVC extensions in your Spring application context for rendering the partial response (note that these extensions require the use of Tiles for templating):
<bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
<property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTilesView"/>
</bean>
This configures the AjaxUrlBasedViewResolver
which in turn interprets Ajax requests and creates FlowAjaxTilesView
objects to handle rendering of the appropriate fragments. Note that FlowAjaxTilesView
is capable of handling the rendering for both Web Flow and pure Spring MVC requests. The fragments correspond to individual attributes of a Tiles view definition. For example, take the following Tiles view definition:
<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>
An Ajax request could specify the "body", "hotelSearchForm" or "bookingsTable" to be rendered as fragments in the request.
Handling Ajax Requests with Spring MVC + Spring Web Flow
Spring Web Flow handles the optional rendering of fragments directly in the flow definition language through use of the render
element. The benefit of this approach is that the selection of fragments is completely decoupled from client-side code, such that no special parameters need to be passed with the request the way they currently must be with the pure Spring MVC controller approach. For example, if you wanted to render the "hotelSearchForm" fragment from the previous example Tiles view into a rich Javascript popup:
<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>
相关推荐
《Spring Boot参考指南》是Spring Boot开发者的重要参考资料,它详细阐述了Spring Boot框架的核心特性、配置方式、自动配置原理以及如何构建微服务应用等关键知识点。Spring Boot是Java开发领域中一个极其流行的轻量...
### Spring Web Flow 2.4.0 Reference Guide关键知识点 #### 一、Spring Web Flow简介与新特性 ##### 1.1 引言 Spring Web Flow(SWF)是Spring框架的一部分,它提供了一种强大的方式来管理Web应用程序中的流程...
《Spring Dynamic Modules Reference Guide中文版》是一份详细阐述Spring Dynamic Modules (Osgi)技术的指南,旨在帮助开发者理解和使用这个框架在OSGi环境下进行开发。Spring Dynamic Modules是Spring框架的一个...
标题“Spring API + Reference”指的是Spring框架的API文档和参考指南。这个压缩包包含了两个重要的文件,即“spring 2.0 api.CHM”和“spring2.0-reference_final_zh_cn.chm”。这两个文件提供了关于Spring框架2.0...
Spring Boot Reference Guide是Spring Boot框架的官方参考文档,它为开发者提供了一个全面的指导,帮助他们理解和使用Spring Boot进行高效开发。Spring Boot是Spring框架的一个模块,它提供了一种快速、简便的Spring...
Spring Security Reference提供了该框架的官方文档,旨在帮助开发人员理解和应用Spring Security来保护他们的应用程序。 1. Spring Security简介: Spring Security是一个为基于Spring的应用程序提供声明式安全...
《Spring 入门教程》是针对初学者设计的一份详细指南,主要涵盖了Spring框架的基础知识,帮助开发者快速理解和掌握Spring的核心概念。这份PDF教程简洁明了,内容精炼,非常适合初次接触Spring的开发者进行自学。 ...
"spring-framework-reference.pdf"同样是Spring 2.5版本的官方文档,虽然格式不同,但内容基本与CHM文件一致。PDF格式可能更适合在不同的设备上阅读,或者方便打印保存。 学习这些文档,开发者可以深入理解Spring...
"spring_api+spring_reference中文版"是一个包含Spring框架API和参考指南的CHM文档集合,为开发者提供了详尽的中文参考资料。 1. **依赖注入(Dependency Injection,DI)**:Spring的核心特性之一,它允许开发者在...
《Spring参考中文版》是Java开发领域中一本重要的技术文档,由知名开发者夏昕翻译,主要涵盖了Spring框架的核心概念、配置、使用方法以及最佳实践。Spring作为Java企业级应用开发的基石,其丰富的功能和强大的灵活性...
本文将深入探讨Spring框架的reference,尤其是基于英文原版文档的理解,以帮助开发者更准确地掌握其精髓。 《Spring Framework Reference》是Spring官方发布的详尽指南,它涵盖了Spring框架的所有主要模块和技术。...
Spring Reference(中文版)是官方文档的中文翻译,为开发者提供了深入理解 Spring 框架的详细指南。 **依赖注入(DI)与控制反转(IoC)** Spring 的核心特性之一是依赖注入(Dependency Injection, DI),也被称为...
### Spring Framework 4.3.19版本关键知识点解析 #### 一、Spring框架概览 **1. 开始使用Spring** - **Spring框架简介**:Spring是一个开源框架,最初由Rod Johnson创建,旨在简化企业级Java应用的开发。Spring...
Spring Boot 2.1.1是该框架的特定版本,而我们所提供的文件"spring-boot-reference.pdf"则是关于这个版本的参考指南。 在【标题】中提到的文档名称“spring-boot-reference.pdf”,表明这是一份Spring Boot 2.1.1...
Spring Framework Reference,Spring框架文档电子书,适合iPad和iPhone等设备翻阅的掌上书籍,英文版。
《Spring Framework Reference》是Spring框架官方的权威指南,它详细阐述了Spring框架的核心特性与使用方法。这本书虽然为英文版,但因其清晰的表述和丰富的示例,即便是非英语母语的学习者也能轻松理解。 Spring ...
Spring_2.5_Reference中文版(包括pdf和chm文档)。