2013-04-01 30 views
8

我使用Primefaces TabView,CommandButton和FileDownload下載日誌文件。一旦日誌文件被下載,我想提供從服務器上刪除日誌內容的選項。文件下載後更新組件

最初,刪除日誌文件按鈕(deleteEventLogButton)被禁用,並具有自定義標題,指出「刪除日誌 - 導出必需」。一旦導出日誌,應該啓用該按鈕,標題應該指出「刪除日誌」。

我遇到的問題是即使在導出事件成功完成後,刪除日誌文件按鈕仍處於禁用狀態,並且標題顯示「刪除日誌 - 導出必需」。

我的猜測是在fileDownload值之前調用了exportEventLogButton-> Update =「deleteEventLogButton」。

一旦我導出了日誌,我可以點擊'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> 

我已經搜索了幾天,現在發現了很多問題,這些問題非常接近這個......但沒有任何幫助。 :(

只是爲了讓事情很清楚...我沒有與出口問題,但問題是,出口完成後刪除日誌文件按鈕不會啓用。

回答

22

你的情況p:commandButton是(一個必須是)非AJAX按鈕(您可以通過添加ajax="false"屬性設置此)。在這種情況下update屬性和p:ajax標籤不有任何意義(因爲它們只適用於AJAX請求)。當你有文件下載時,你的應用程序發送一些類型的流,y請參閱保存文件對話框。你的頁面沒有刷新。所以,你必須使用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"/> 
+1

太棒了!這工作很好。我喜歡這個網站......這是因爲像你這樣的人! – AceFunk

+0

因爲你是新的,如果這有助於你,你應該接受一個答案 – partlov

+0

它的工作!謝謝。 –

1

您是否嘗試過改變eventLogExported/isEventLogExported從布爾布爾或字符串?

+0

我現在...但它沒有任何區別。 (還是)感謝你的建議。 – AceFunk

1

由於Balusc回答,在questionrevisions),我們無法獲得響應兩次一個單一的請求,下載後刷新頁面,更好地使用下面的Java腳本在下載鏈接(p:commandbutton)onclick標記。

實施例:

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

這將自動刷新頁面1秒後,在同一時間,即在刷新之前,您將根據您的下載響應時間獲取下載文件,並增加該腳本中的秒數。 秒不應少於下載響應時間。