【问题标题】:show working image while executing h:commandlink action在执行 h:commandlink 操作时显示工作图像
【发布时间】:2014-12-05 14:44:24
【问题描述】:

要添加一篇文章,我有 <h:commandlink> 执行一个操作,该操作根据操作结果重定向到成功或错误页面。该操作需要一段时间才能完成执行,大约需要 4 秒。你能告诉我如何在动作开始执行时显示加载图像并在动作终止时隐藏它吗?

【问题讨论】:

  • 既然你的问题基本上是有可能...正确答案应该是是的,有可能
  • "显示加载图片?" 如果你坚持使用 PrimeFaces 等丰富的组件库,那么它们是:<p:blockUI>, <pe:blockUI>, <p:ajaxStatus>
  • 我有一个答案,但意味着在处理成功后隐藏 gif。我不知道如何发送重定向,因为我不知道如何使用 hack 将正确的数据设置为 w/o
  • 我不要求重定向,因为我知道该怎么做。
  • 我只是要求为 a4j:commandlink 提供类似 onbegin 和 oncomplete 的功能,但我不想使用richfaces。

标签: javascript jsf jsf-2


【解决方案1】:

您将需要使用 ajax 来完成手头的任务。使用普通的 JSF 和一些 javascript,这将是解决方案:

<h:form>
    <h:commandLink value="Submit" action="#{yourManagedBean.desiredAction}">
        <f:ajax onevent="showProgress" />
    </h:commandLink>
    <!-- other components... -->

    <!-- External div which wraps the loading gif -->
    <div id="divProgress" style="display: none; height: 60px; overflow: hidden;">
        <div style="display: table-cell; vertical-align: text-top;">
            Working...
            <!-- Locate your gif wherever you stored -->
            <h:graphicImage url="resources/images/loading.gif" height="49" width="49" />
        </div>
    </div>

    <script type="text/javascript">
        function showProgress(data) {
            var ajaxStatus = data.status;
            switch (ajaxStatus) {
                case "begin":
                    //This is called right before ajax request is been sent.
                    //Showing the div where the "loading" gif is located
                    document.getElementById("divProgress").style.display = 'table';
                    break;

                case "success":
                    //This is called when ajax response is successfully processed.
                    //In your case, you will need to redirect to your success page
                    var url = window.location.protocol + "//"
                        + window.location.host + "/"
                        + (window.location.port ? ":"+ window.location.port: "");
                    window.location.href = url + "#{request.contextPath}/" + "#{yourManagedBean.resultUrl}";
                    break;
            }
        }
    </script>
</h:form>

在您的托管 bean 中:

@ManagedBean
@ViewScoped
public class YourManagedBean {
    private String resultUrl;
    //getters and setters...
    public void desiredAction() {
        //do the processing...
        if (...) {
            resultUrl = "success.xhtml";
        } else {
            resultUrl = "error.xhtml";
        }
    }
}

【讨论】:

  • 感谢您的回答,我对 h:commandLink 的 onClick 事件做了同样的事情,它按我的意愿工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-09
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 1970-01-01
  • 2011-08-14
相关资源
最近更新 更多