Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"/>
                <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.

like image 915
d1van Avatar asked Nov 13 '22 18:11

d1van


1 Answers

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

like image 199
Nawa Avatar answered Dec 20 '22 23:12

Nawa