JavaServer Faces Technology.
Before going on, you might want to skim through this article to get an overview of what Dynamic Faces is all about: New Technologies for Ajax and Web Application Development: Project Dynamic Faces.
Throughout this blog series, I'll use a simple example that uses standard JavaServer Faces components along with Dynamic Faces to allow users to do the following:
- Select a fruit from a radio button group and instantly see both the varieties for that fruit appear in a menu and a description of the currently selected variety display without experiencing a full-page refresh.
- Select one of the varieties from the menu to see its description, also without experiencing a page refresh.
Here's a screenshot:
If you were using plain JavaServer Faces technology without Dynamic Faces, you would need to add a button to update the varieties menu and the variety description, and you would have to go through a full-page refresh to do the update.
With Dynamic Faces, you can get rid of the button and rely on Dynamic Faces to do the work of updating these components using Ajax.
To see this example in action, download
simpleDynamicFaces.war
from the samples folder of the JSF Extensions project, which includes Dynamic Faces, and perform the simple set-up steps described in Setting up an Application to Use Dynamic Faces in the article mentioned previously. After that, you're ready to start coding.
Behind the scenes, Dynamic Faces performs all the Ajax functionality through the use of a set of JavaScript libraries. One of the JavaScript functions that Dynamic Faces provides is the
fireAjaxTransaction
function. As its name suggests, it fires an Ajax request in response to a particular event, such as clicking on a component. As such, this function gives you component-level control over what is updated in your page. To use the
fireAjaxTransaction
function, you do the following:
The following piece of the JSP page shows how I used
fireAjaxTransaction
to update components via Ajax, as shown on lines 10 and 16. Notice on lines 11 and 17 that I have used the standard
valueChangeListener
tag attributes to register value-change events on the
fruit
and
variety
components. I'll get to the importance of this later on.
-
<f:view>
-
...
-
<h:form prependId="false" >
-
<h:panelGrid columns="2" cellpadding="4">
-
-
<h:outputText value="Select a fruit:"/>
-
<h:outputText value="Select a variety:"/>
-
-
<h:selectOneRadio value="#{fruitInfoBean.fruit}"
-
onclick="DynaFaces.fireAjaxTransaction(this, { execute: 'fruit'});"
-
valueChangeListener="#{fruitInfoBean.changeFruit}">
-
<f:selectItems value="#{fruitInfoBean.fruits}"/>
-
</h:selectOneRadio>
-
-
<h:selectOneMenu value="#{fruitInfoBean.variety}"
-
onchange="DynaFaces.fireAjaxTransaction(this, { execute: 'variety' });"
-
valueChangeListener="#{fruitInfoBean.updateVariety}">
-
<f:selectItems value="#{fruitInfoBean.varieties}"/>
-
</h:selectOneMenu>
-
-
</h:panelGrid>
-
-
<h:outputText value="#{fruitInfoBean.varietyInfo}" />
-
-
</h:form>
-
</f:view>
The first thing I did in the page is I set the
form
tag's
prependId
attribute to false so that I can refer to component IDs without prepending the form's ID. This attribute was added in JavaServer Faces technology 1.2. In Ajax applications, you often have to refer to client IDs. Without the
prependId
attribute, you'd have to add the form ID to every client ID reference, which adds extra bytes to network transactions and is a pain to type. Remember, with Ajax, you are doing more transactions, so you want each one to be as small as possible.
After setting the
prependId
attribute, I added an
onclick
attribute to the
fruit
selectOneRadio
tag and set it to the following call to
fireAjaxTransaction
:
"DynaFaces.fireAjaxTransaction(this, { execute: 'fruit'});"The
this
parameter is a JavaScript reference that is the DOM element for the markup generated by the
fruit
selectOneRadio
tag.
The other parameter is a kind of JavaScript Object known as an associative array, in which the keys are strings and the values are whatever you want them to be. Each key/value pair represents an option that you pass to the
fireAjaxTransaction
function.
These options tell Dynamic Faces which parts of the component tree it needs to process and re-render using Ajax. The Dynamic Faces JavaScript Library Reference includes the complete list of acceptable options.
In this case I have only one option,
execute: 'fruit'
, which says that the
fruit
component (and its children, if it has any) must go through the execute portion of the JavaServer Faces life cycle. This part of the life cycle includes the phases responsible for converting and validating data, updating model values, and handling action events. I did not include the render option because I want all components to be re-rendered as a result of this action, which is the default behavior.
As shown on line 10 of the preceding JSP page, the
fireAjaxTransaction
function is called when the user clicks on a radio button. When this happens, the
fruit
component goes through the execute phase of the life cycle, and the value-change event that is registered on it is fired.
The
valueChangeListener
attribute of the
fruit
component tag references the
changeFruit
method, which handles the value-change event (see FruitInfoBean.java). The
changeFruit
method performs some model updates when the
fruit
component is activated. Therefore, the
fruit
component must go through the execute phase of the life cycle so that these model updates will occur.
The
changeFruit
method updates the model value of the
variety
menu component with the list of varieties corresponding to the fruit the user chose from the
fruit
component. For example, if the user selects Pear from the
fruit
component, the
variety
component will display the values Aurora, Bartlet, Bosc, Comice, and Delicious.
The
changeFruit
method also sets the currently selected fruit value to the first variety in the list. Finally, it updates the
varietyInfo
output component so that the description of a variety corresponds to the currently selected fruit variety. For example, if the user chooses Pear, Aurora is the selected value of the
variety
component, and the
varietyInfo
component displays this message:
Aurora: Sweet, juicy, and aromatic. Quality dessert pear ...
When Dynamic Faces re-renders these components using Ajax, the components will display with the new values.
I also used the
fireAjaxTransaction
function with the
variety
component:
"DynaFaces.fireAjaxTransaction(this, { execute: 'variety'});"
This function call works the same way as it does for the
fruit
component: It causes the
variety
component to go through the execute phases of the life cycle and re-renders all components via Ajax.
As with the
fruit
component, the
variety
component has a value-change event registered on it. Like the
changeFruit
method, the
updateVarety
method, which handles this event also updates model values. The
updateVariety
method updates the
varietyInfo
component's model value so that the displayed description of a fruit variety corresponds to the variety the user selected from the
variety
component menu.
That's all there is to it. For this example, you do not need to write any JavaScript code to use Ajax, nor do you need to do anything special in your Java code. And the best part is that you can use the JavaServer Faces components you already know and use without modifying them.
相关推荐
**Ajaxify一个简单的HTML页面使用Web服务** Ajax(Asynchronous JavaScript and XML)是一种在无需刷新整个网页的情况下,能够更新部分网页的技术。它通过在后台与服务器进行少量数据交换,使网页实现异步更新,...
**Ajaxify: 一个强大的Ajax插件** Ajaxify是一款针对JavaScript设计的插件,它旨在为网站提供平滑、无刷新的用户体验。通过利用Ajax技术,Ajaxify可以在不重新加载整个页面的情况下,更新页面的部分内容,提升用户...
Ajax-ajaxify.zip,提出异步请求,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载网页的情况下更改。
【Ajaxify:开启前端项目的全新体验】 Ajaxify是一款旨在提升前端项目用户体验的解决方案,通过将网页无刷新加载技术引入到项目中,使用户在浏览网站时享受到流畅、快速且连贯的交互体验。"开箱即用"的概念意味着...
Testing lessons show you how to eliminate cross-browser JavaScript errors and DOM debugging nightmares using a combination of Firebug, and Venkman. Advanced material explains the most current design ...
**Ajaxify: 移至Ajaxinate - Shopify主题的Ajax分页增强** Ajaxify是一个专门针对Shopify主题设计的Ajax分页插件。它的主要目的是提升用户的浏览体验,通过使用Ajax技术来实现页面内容的无刷新加载,使得用户在翻阅...
Ajaxify 是一款基于 JavaScript 的插件,用于将网站页面无刷新地加载,提供更加流畅的用户体验。原版的 HTML5 Ajaxify 插件旨在利用 HTML5 的 History API 和 Ajax 技术来实现页面间的平滑导航,而"ajaxify:对 ...
云雀-ajaxify-路由器 精心设计的前端路由框架。 特征 支持Hashbang,HTML5 pushstate,内部实现三种路由模式,该路由模式可以按属性设置或由框架自动设置,而不会影响应用程序的实现。 支持全局事件以实现挂钩函数...
指数正在安装通过NPM分发的库: $ npm install better-ajaxify --save-dev 这会将最新版本的Better-ajaxify克隆到项目根目录的node_modules目录中。 然后在页面上附加以下html元素: < script src =" node_...
本文将深入探讨如何利用这些技术,特别是如何通过Ajaxify方法使您的控件在ASP.NET WebForms环境中实现无刷新更新。 标题“动态用户控件,Ajaxify您的控件”指的是在Web应用程序中使用动态创建和管理的用户控件,并...
Ajaxify-any-website 用于将所有内部链接加载到您网站的 ajax 的简单代码。 只需在您通常在网站中执行的位置添加此 javascript 代码并添加一个新的 就在你的里面 (或您称之为主要内容的任何内容)包装器。 确保...
这是一个片段,可以让您了解如何使用 CMB2 'Ajaxify' 下拉列表。 任何问题都可以随时提出,我会尽快回答。 免责声明 我意识到每个人对事物的编码都不一样,所以如果这是“错误的”或者你有更好的方法,请提交 PR ...
Rekord - Ajaxify Music - Events - Podcasts Multipurpose WordPress Theme Rekord - ajaxify音乐 - 事件 - 播客多功能Wordpress主题" ---------- 泰森云每天更新发布最新WordPress主题、HTML主题、WordPress插件...
Ajax 化 Rails 您的 Rails 应用程序不再需要重新加载整页! 耶! 通过 Ajax 自动使您的应用程序在后台加载内容。 通过将所有内部链接转换为触发页面内容区域更新的 Ajax 链接来工作。 表单提交也会自动转换为 ...
云雀-ajaxify-路由器 一个精心设计的前端路由框架。 特征 支持Hashbang、HTML5 pushstate、内部实现三种路由模式,路由模式可以通过属性设置也可以由框架自动设置,不影响应用的实现。 支持全局事件来实现钩子功能...
input type="hidden" name="return_to" value="/checkout">注入添加到购物车表单,或使用随时将任意数量的商品添加到购物车。 为了使本教程适合您,您需要在主题中禁用 Ajax。 转到您的自定义主题页面,查看...
在 Ruby 或 Javascript 中构建一个 Web 服务(例如,一个响应 JSON 的服务)以转换任意文本,使其在句子之间的... 运行: 1: 克隆 repo 1.5: cd into repo 2: bundle install 3: be shotgun server.rb 4: 转到托管页面
加载更多任何WordPress插件一个简单的插件,可帮助您使用jquery加载更多项目。 您可以对博客文章,评论,页面,类别,最近的文章,侧边栏小部件数据,Woocommerce产品,图像,照片,视频,自定义Div或任何您想要的...
Ajaxify() -类似于$.ajax() DOM选择器,例如$('a') , $('.container') , $(document) $(document).ready(function(){})功能在DOM准备好后加载 同级和父级选择器, next() , prev() , parent() , children() ...