发出 JSF Ajax 请求时显示加载进度

发布于 2024-12-11 19:18:39 字数 57 浏览 0 评论 0原文

使用 发出请求时如何显示一些加载消息?

How can I show some loading message when making request using <f:ajax>?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤 2024-12-18 19:18:39

如果您尚未使用第三方组件库,该库可能已经有现成的组件,例如 PrimeFaces,那么您可以使用 JSF 提供的 JavaScript faces.ajax.addOnEvent() 函数(最终还有 faces.ajax.addOnError()< /a>) 挂钩 ajax 事件上的函数。

这是一个基本的启动示例:

<script>
    faces.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;" />

另请参阅 13.3.5.2 rel="nofollow noreferrer">Jakarta Faces 4.0 规范

13.3.5.2 监控所有 Ajax 请求的事件

JavaScript API 提供了faces.ajax.addOnEvent 函数,可用于注册 JavaScript 函数
当任何 Ajax 请求/响应事件发生时都会收到通知。请参阅第 14.4 节“注册回调
功能”了解更多详细信息。 faces.ajax.addOnEvent 函数接受 JavaScript 函数参数,该参数将
在任何 Ajax 请求/响应事件周期期间发生事件时收到通知。实施必须
确保注册的 JavaScript 函数必须根据中概述的事件进行调用
表 14-3 节“事件”。

注意:如果您仍在使用 JSF 3.0 或更早版本,请使用 jsf.ajax 命名空间,而不是 faces.ajax,如 jsf.ajax.addOnEvent( ...)

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 faces.ajax.addOnEvent() function (and eventually also faces.ajax.addOnError()) to hook a function on ajax events.

Here's a basic kickoff example:

<script>
    faces.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;" />

See also section 13.3.5.2 of the Jakarta Faces 4.0 specification:

13.3.5.2 Monitoring Events For All Ajax Requests

The JavaScript API provides the faces.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. The faces.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”.

Note: in case you're still using JSF 3.0 or older, then use jsf.ajax namespace instead of faces.ajax as in jsf.ajax.addOnEvent(...).

那伤。 2024-12-18 19:18:39

richfaces 有一个非常易于使用的组件,我这样使用:

<a4j:status startText="" stopText="" onstart="showAjaxActive();" onstop="hideAjaxActive();"/>

richfaces has a very easy to use component that I use like this:

<a4j:status startText="" stopText="" onstart="showAjaxActive();" onstop="hideAjaxActive();"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文