Primefaces problem: p:filedownload from p:datatable with ViewScoped managed bean
p:filedownload from p:datatable with ViewScoped managed bean wont work. It calls the methods prepareFile and getFile twice. In first call of the methods I mentioned it sets the first file from the table, and in the second call of the methods it sets the right file, but it always downloads only the first one and the second one is never downloaded.
Why does it call twice? Why does it set the first file from the table? Any ideas?
Here's my code:
<p:dataTable id="offer_attachment_datatable"
widgetVar="offer_attachment_datatable"
var="attachment"
value="#{offerBean.offerAttachments}">
<p:column>
<f:facet name="header"/>
开发者_Python百科 <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>
and in managed bean (simplified):
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;
}
So, I made a workaround with a simple p:confirmDialog where I extracted the problematic ajax=false command link, so I select the attachment by clicking it in p:datatable and execute the download from the p:confirmdialog.
I had the same problem in 2.2.1. I found the solution by replacing p:commandLink
to p:commandButton
with the same attributes. Seems that it is a bug related with behavior of the commandLink
component
Ok, guys, so I made a workaround with a simple p:confirmDialog where I extracted the problematic ajax=false command link, so I select the attachment by clicking it in p:datatable and execute the download from the p:confirmdialog.
The solution that worked for me was to replace "p:datatable" with "ui:repeat(facelets) and table", like this:
<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>
精彩评论