14

我正在使用 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>

支持 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>

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

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

4

4 回答 4

37

p:commandButton在您的情况下是(必须是)非 AJAX 按钮(您通过添加ajax="false"属性来设置它)。在这种情况下,update属性和p:ajax标签没有任何意义(因为它们仅适用于 AJAX 请求)。当您下载文件时,您的应用程序会发送某种类型的流,您会看到“保存文件”对话框。您的页面未刷新。所以你必须使用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"/>
于 2013-04-01T14:23:54.417 回答
4

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

例子:

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

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

于 2015-04-09T05:22:10.750 回答
2

从 PrimeFaces 10,您可以下载带有 ajax="true". 这也允许您更新组件。

也可以看看:

于 2021-09-24T14:24:52.633 回答
0

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

于 2013-04-01T14:16:04.963 回答