发出 JSF Ajax 请求时显示加载进度 [英] Show loading progress when making JSF Ajax request
问题描述
如何在使用
发出请求时显示一些加载消息?
How can I show some loading message when making request using <f:ajax>
?
推荐答案
如果您还没有使用第 3 方组件库,该库可能已经有一个现成的组件,例如 PrimeFaces 和 <p:ajaxStatus>
,然后就可以使用JSF提供的JavaScript jsf.ajax.addOnEvent()
函数(最终还有 jsf.ajax.addOnError()
) 挂钩ajax 事件函数.
If you're not already using a 3rd party component library which could already have a ready-made component for that, such as PrimeFaces with <p:ajaxStatus>
, then you can use the JSF-provided JavaScript jsf.ajax.addOnEvent()
function (and eventually also jsf.ajax.addOnError()
) to hook a function on ajax events.
这是一个基本的开球示例:
Here's a basic kickoff example:
<script>
jsf.ajax.addOnEvent(function(data) {
var ajaxstatus = data.status; // Can be "begin", "complete" and "success"
var ajaxloader = document.getElementById("ajaxloader");
switch (ajaxstatus) {
case "begin": // This is called right before ajax request is been sent.
ajaxloader.style.display = 'block';
break;
case "complete": // This is called right after ajax response is received.
ajaxloader.style.display = 'none';
break;
case "success": // This is called when ajax response is successfully processed.
// NOOP.
break;
}
});
</script>
<img id="ajaxloader" src="ajaxloader.gif" style="display: none;" />
另请参阅 JSF 2.0 规范的第 13.3.5.2 章:
See also chapter 13.3.5.2 of the JSF 2.0 specification:
JavaScript API 提供了 jsf.ajax.addOnEvent
函数,可用于注册 JavaScript 函数当任何 Ajax 请求/响应事件发生时,它会得到通知.请参见第 14.4 节注册回调功能"了解更多详情.jsf.ajax.addOnEvent
函数接受一个 JavaScript 函数参数,它将是在任何 Ajax 请求/响应事件周期中发生事件时通知.实施必须确保必须根据中概述的事件调用注册的 JavaScript 函数第 14-3 节事件".
13.3.5.2 Monitoring Events For All Ajax Requests
The JavaScript API provides the
jsf.ajax.addOnEvent
function that can be used to register a JavaScript function that will be notified when any Ajax request/response event occurs. Refer to Section 14.4 "Registering Callback Functions" for more details. Thejsf.ajax.addOnEvent
function accepts a JavaScript function argument that will be notified when events occur during any Ajax request/response event cycle. The implementation must ensure the JavaScript function that is registered must be called in accordance with the events outlined in Section TABLE 14-3 "Events".
您可以从 http://www.ajaxload.info 免费获取一些很酷的 ajax 加载程序 gif,来自方式.
You can grab some cool ajax loader gifs for free from http://www.ajaxload.info, by the way.
这篇关于发出 JSF Ajax 请求时显示加载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!