2011-08-28 60 views
5

p:使用ViewScoped託管bean的p:datatable下載文件不會工作。它調用方法prepareFile和getFile兩次。在我提到的方法的第一次調用中,它從表中設置第一個文件,並在第二次調用方法時設置正確的文件,但它總是隻下載第一個文件,而第二個永遠不會下載。Primefaces問題:p:使用ViewScoped託管bean從p:datatable下載文件

它爲什麼要調用兩次?爲什麼它會設置表中的第一個文件?有任何想法嗎?

這裏是我的代碼:

<p:dataTable id="offer_attachment_datatable" 
        widgetVar="offer_attachment_datatable" 
        var="attachment" 
        value="#{offerBean.offerAttachments}"> 
      <p:column> 
       <f:facet name="header"/> 
       <p:commandLink ajax="false" actionListener="#{offerBean.prepareFile(attachment)}" title="#{attachment.name}"> 
        <p:graphicImage value="/resources/themes/navigator_b2e/images/drive-download.png" /> 
        <p:fileDownload value="#{offerBean.file}"/> 
       </p:commandLink> 
      </p:column> 
</p:dataTable> 

和管理的bean(簡體):

private StreamedContent file; 
private InputStream stream; 

public void prepareFile(OfferAttachment attachment){ 
    System.out.println("Attachment: "+attachment.getName()); 
    stream = new ByteArrayInputStream(attachment.getAttachment()); 
    file = new DefaultStreamedContent(stream, "text/plain", attachment.getName()); 
    stream = null; 
} 

public StreamedContent getFile() { 
    System.out.println("File: "+file.getName()); 
    return file; 
} 

public void setFile(StreamedContent file) { 
    this.file = file; 
} 

所以,我做了一個變通方法用一個簡單的號碼:confirmDialog哪裏提取的問題AJAX = FALSE命令鏈接,因此我通過在p:datatable中單擊來選擇附件,然後從p:confirmdialog執行下載。

+0

我也試過傳遞rowIndex作爲af:param,但它總是發送相同的rowIndex:只有第一個點擊。我認爲問題可能在於此ajax = false,但我不確定...我正在使用PrimeFaces 2.2.1。和Glassfish 3.1 – d1van

+0

現在看起來這個bean被破壞了。它下載正確的文件,但是當我嘗試了一些操作後,我得到 'SEVERE:com.sun.faces.mgbean.ManagedBeanCreationException:在託管bean offerBean上執行資源注入時發生錯誤 – d1van

回答

1

我在2.2.1中遇到了同樣的問題。我找到了解決方案,使用相同的屬性替換p:commandLinkp:commandButton。看來,這是一個與commandLink組件

0

爲我工作的解決方案的行爲相關的是,以取代錯誤:「(小面)表重複和UI」,像這樣:

<table role="grid"> 
<thead> 
    <tr role="row"> 
     <th>File Name</th> 
     <th>Action</th> 
    </tr> 
</thead> 
<tbody> 
    <ui:repeat value="#{downloadFileBean.files}" var="f"> 
     <tr role="row"> 
      <td><h:outputText value="#{f.name}" /></td> 
      <td> 
       <p:commandLink id="download" ajax="false"> 
        <h:outputText value="Download" /> 
        <p:fileDownload value="#{downloadFileBean.file}" /> 
        <f:param name="fileName" value="#{f.name}" /> 
       </p:commandLink> 
      </td> 
     </tr> 
    </ui:repeat> 
</tbody> 
「P數據表」與

相關問題