Primefaces问题:p:使用ViewScoped托管bean从p:datatable下载文件 [英] Primefaces problem: p:filedownload from p:datatable with ViewScoped managed bean

查看:153
本文介绍了Primefaces问题:p:使用ViewScoped托管bean从p:datatable下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

p:使用ViewScoped托管bean的p:dataatable下载不会工作。它调用方法prepareFile和getFile两次。在我提到的方法的第一个调用中,它从表中设置第一个文件,在第二个调用方法中它设置正确的文件,但它总是只下载第一个文件,第二个永远不会下载。 >

为什么叫两次?为什么它从表中设置第一个文件?任何想法?



以下是我的代码:

 < p: dataTable id =offer_attachment_datatable
widgetVar =offer_attachment_datatable
var =attachment
value =#{offerBean.offerAttachments}>
< p:栏>
< f:facet name =header/>
< p:graphicImage value =/ resources / themes / navigator_b2e / images / drive-download.png/>
< p:fileDownload value =#{offerBean.file}/>
< / p:commandLink>
< / p:列>
< / p:dataTable>

和托管bean(简体):

 私有的StreamedContent文件; 
私人InputStream流;

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;

$ b $ public StreamedContent getFile(){
System.out.println(File:+ file.getName());
返回文件;
}

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



$ b $ p
$ b所以,我用一个简单的p:confirmDialog做了一个解决方法,有问题的ajax = false命令链接,所以我通过在p:datatable中单击来选择附件,然后从p:confirmdialog执行下载。

解决方案 p:commandLink p:commandButton 找到了解决方案。看来这是一个与 commandLink 组件的行为有关的错误


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.

解决方案

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

这篇关于Primefaces问题:p:使用ViewScoped托管bean从p:datatable下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆