【发布时间】:2011-06-01 14:53:36
【问题描述】:
我正在将 PrimeFaces 用于一个新项目,它是一组令人印象深刻的组件。 无论如何,我对文件下载组件的“现实世界”使用有疑问。 在我的页面中,我有一个显示与特定文档相关的附件的数据列表,我想提供一个链接以直接在数据列表项中下载该文件。 这是我的 xhtml 代码:
<p:dataList id="ListaAllegati" value="#{documentBean.documento.allegati}" type="definition" var="attach" style="border: none" ">
<f:facet name="description">
<h:outputText value="#{attach.name}" />
<p:commandLink ajax="false" title="Download" action="#{documentBean.selectAttach}>
<h:graphicImage style="margin-left: 10px; border: none" value="./images/article.png" height="24" width="24" ></h:graphicImage>
<p:fileDownload value="#{documentBean.downloadFile}"/>
<f:setPropertyActionListener target="#{documentBean.selectedAttach}" value="#{attach}" />
</p:commandLink>
</f:facet>
</p:dataList>
和相关的java bean(请求范围):
private StreamedContent downloadFile;
public StreamedContent getDownloadFile() {
log.info("getter dell'allegato invocato");
InputStream stream = null;
byte[] rawFile = null;
if (selectedAttach == null) {
log.warn("Nessun allegato passato");
return null;
} else {
try {
log.info("Recupero del file " + selectedAttach.getGuid());
rawFile = attachManager.retrieveFile(selectedAttach.getGuid());
} catch (Exception e) {
String msg = "Errore durante il recupero del file";
log.error(msg, e);
FacesMessage fmsg = new FacesMessage(msg, "");
FacesContext.getCurrentInstance().addMessage(null, fmsg);
}
stream = new ByteArrayInputStream(rawFile);
DefaultStreamedContent file = new DefaultStreamedContent(stream,
selectedAttach.getMimeType(), selectedAttach.getName());
return file;
}
}
public void selectAttach() {
log.info("commandLink action invocata");
}
private Allegato selectedAttach;
public Allegato getSelectedAttach() {
return selectedAttach;
}
public void setSelectedAttach(Allegato selectedAttach) {
log.info("Allegato selezionato");
if (selectedAttach==null) log.warn("L'allegato passato è nullo");
this.selectedAttach = selectedAttach;
}
所以,有几个问题:
- 尝试以这种方式传递选定的附件是否正确?否则,我如何传递一个参数来告诉 bean 附件已被点击?
- 为什么我第一次单击命令链接时没有任何反应?它与服务器进行往返,但没有任何反应。第二次,它给了我一个例外。
- 为什么从不调用 documentBean.selectAttach 并且从不设置 documentBean.selectedAttach 属性(第二次都没有)?
感谢任何人的任何提示
【问题讨论】:
标签: jsf download primefaces