【问题标题】:Update component after file download文件下载后更新组件
【发布时间】:2013-03-22 14:32:56
【问题描述】:

我正在使用 Primefaces TabView、CommandButton 和 FileDownload 下载日志文件。下载日志文件后,我想提供从服务器中删除日志内容的选项。

最初,删除日志文件按钮 (deleteEventLogButton) 被禁用,并有一个自定义标题说明“删除日志 - 需要导出”。导出日志后,应启用该按钮,并且标题应为“删除日志”。

我遇到的问题是删除日志文件按钮仍处于禁用状态,并且即使在导出事件成功完成后,标题仍显示为“删除日志 - 需要导出”。

我的猜测是 exportEventLogButton->Update="deleteEventLogButton" 在 fileDownload 值之前被调用。

导出日志后,我可以按“F5”并刷新页面,deleteEventLogButton 会启用并显示正确的标题。

JSF - 片段

<p:tabView id="logView">
    <p:tab id="eventLogTab" title="Security Events">
        <p:panelGrid ...>

            <p:commandButton id="exportEventLogButton" icon="ui-icon-disk" styleClass="c25" ajax="false" title="Export Log" disabled="#{empty managedCmsLogsBean.eventLogEntityList}" update="deleteEventLogButton">
                <p:fileDownload value="#{managedCmsLogsBean.exportEventLogFiles()}"/>
            </p:commandButton>

            <p:commandButton id="deleteEventLogButton" icon="ui-icon-trash" styleClass="c25" ajax="false" title="#{managedCmsLogsBean.deleteEventLogCaption}" disabled="#{! managedCmsLogsBean.eventLogExported}" action="#{managedCmsLogsBean.clearEventLogs()}" update="eventLogTab" />    

        </p:panelGrid>

        <p:dataTable value="#{managedCmsLogsBean.eventLogEntityList}" ...>
            ...
        </p:dataTable>

    </p:tab>
</p:tabView>

Backing Bean - 片段

private boolean eventLogExported;

public StreamedContent exportEventLogFiles() {
    eventLogExported = true;
    return logFileUtility.exportSecurityEventLog(eventLogEntityList, eventLogStartDate, eventLogStopDate);
}

public boolean isEventLogExported() {
    return eventLogExported;
}

public void setEventLogExported(boolean value) {
    eventLogExported = value;
}

public String getDeleteEventLogCaption() {
    return eventLogExported ? "Delete Logs" : "Delete Logs - Export Required";
}

我尝试在 FileDownload 中移动更新事件,但没有任何作用。

<p:commandButton id="exportEventLogButton" icon="ui-icon-disk" styleClass="c25" ajax="false" title="Export Log" disabled="#{empty managedCmsLogsBean.eventLogEntityList}">
    <p:fileDownload value="#{managedCmsLogsBean.exportEventLogFiles()}">
        <p:ajax update="deleteEventLogButton"/>
    </p:fileDownload>
</p:commandButton>

我已经搜索了几天,发现了许多与这个问题非常接近的问题......但没有任何帮助。 :(

只是为了让事情变得非常清楚......我没有遇到导出问题。问题是导出完成后删除日志文件按钮没有启用。

【问题讨论】:

    标签: jsf-2 primefaces download


    【解决方案1】:

    您是否尝试将 eventLogExported/isEventLogExported 从 boolean 更改为 Boolean 或 String ?

    【讨论】:

    • 我现在有...但没有任何区别。不过感谢您的建议。
    【解决方案2】:

    p:commandButton 在您的情况下是(必须是)非 AJAX 按钮(您可以通过添加 ajax="false" 属性来设置它)。在这种情况下,update 属性和p:ajax 标记没有任何意义(因为它们仅适用于 AJAX 请求)。当您下载文件时,您的应用程序会发送某种类型的流,您会看到 Save File 对话框。您的页面未刷新。所以你必须使用PrimeFaces.monitorDownload 来做到这一点:

    <p:commandButton id="exportEventLogButton" 
                     icon="ui-icon-disk" 
                     styleClass="c25" 
                     ajax="false" 
                     title="Export Log" 
                     disabled="#{empty managedCmsLogsBean.eventLogEntityList}"
                     onclick="PrimeFaces.monitorDownload(null, stop)">
    

    并添加将更新第二个按钮的停止功能:

    <p:remoteCommand name="stop" update="deleteEventLogButton"/>
    

    【讨论】:

    • 太棒了!这很好用。我喜欢这个网站...这是因为像你这样的人!
    • 因为你是新手,如果这对你有帮助,你应该接受一个答案
    • 谢谢!!它也对我有用,在我的情况下,我必须在 中包含 ajax="true"
    【解决方案3】:

    正如Balusc 回答的那样,在question (revisions) 中,我们无法从单个请求中获得两次响应,下载后刷新页面,最好在下载链接中使用以下java脚本(p:commandbutton) onclick 标记。

    例子:

    <p:commandButton ajax="false" icon="ui-icon-arrowstop-1-s" onclick="setTimeout('location.reload();', 1000);" action="#{managedBean.downloadMethod}" />
    

    这将在 1 秒后自动刷新页面,同时,即在刷新之前,您将获得下载文件,根据您的下载响应时间,增加该脚本中的秒数。 秒数不应少于下载响应时间。

    【讨论】:

      【解决方案4】:

      从 PrimeFaces 10,您可以使用 ajax="true" 下载文件。这也允许您更新组件。

      另见:

      【讨论】:

        猜你喜欢
        • 2013-09-03
        • 2011-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多