Primefaces从数据表下载文件
我遇到了一个奇怪的问题,即未从XHTML调用文件下载"选项.我是JSF的新手,所以我可能搞砸了一些基本的知识,但是对您的帮助将不胜感激.
I have this weird issue with File Download option not being called from the XHTML. I am very new to JSF, so it might be something basic that I am screwing up, but any help would be appreciated.
这是我的XHTML
<p>Files List</p>
<h:form prependId="false">
<p:dataTable value="#{pc_ArchiveFiles.archiveFiles}" var="fs"
paginator="true" rows="5"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<f:facet name="header">
Client Files
</f:facet>
<p:column>
<f:facet name="header">
File Name
</f:facet>
<h:outputText value="#{fs.fileName}" />
</p:column>
<p:column>
<f:facet name="header">
File Size
</f:facet>
<h:outputText value="#{fs.fileSizeInKB}" />
</p:column>
<p:column>
<f:facet name="header">
Download File
</f:facet>
<p:commandLink id="downloadLink" value="Download" ajax="false">
<p:fileDownload value="#{pc_ArchiveFiles.downloadPDF}" />
</p:commandLink>
</p:column>
</p:dataTable>
</h:form>
这是支持豆
@ManagedBean(value = "pc_ArchiveFiles")
@RequestScoped
public class ArchiveFiles extends PageCodeBase {
private static final Logger logger = LoggerFactory
.getLogger(ArchiveFiles.class);
@Value("${archive.location}")
private String repository;
public List<ArchiveFile> getArchiveFiles() {
Map<String, String> params = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
subCategory = params.get("categoryName");
// Build request object
ArchiveFilesRequest request = new ArchiveFilesRequest();
request.setCaseWorkerId(SecurityUtil.getLoggedInUser().getLoggedUserId());
request.setClientId(getSelectedConsumer().getConsumerId());
request.setCategoryName(subCategory);
ArchiveFilesResponse response = archiveService.getArchiveFiles(request);
if((response.getResponseType() == ResponseType.SUCCESS || response.getResponseType() == ResponseType.WARNING) && response.getFileCount() > 0) { // There is at least one file
archiveFiles = new ArrayList<ArchiveFile>();
archiveFiles.addAll(response.getFileSet());
return archiveFiles;
} else {
return Collections.<ArchiveFile> emptyList();
}
}
public void downloadPDF() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
facesContext.responseComplete();
}
}
当我单击下载时,我希望调用downloadPDF().但是,似乎要再次调用getArchiveFiles()方法,而不是downloadPDF().我已将p:CommandLink更改为以下代码,但它仍未调用正确的方法.最重要的是,如果可以正常运行,我还必须传递filename参数.
When I click on download, I want the downloadPDF() called. But it seems to be calling getArchiveFiles() method again instead of downloadPDF(). I have changed p:CommandLink to the below code, but it still does not call the right method. On top of it I have to pass in the filename parameter as well if I get this working.
<h:commandLink id="downloadLink" value="Download PDF" target="_blank" action="#{pc_ArchiveFiles.downloadPDF}" />
p:fileDownload 的
值属性应该是StreamedContent的实例 . 在第174页的此处中检入 它应该类似于下面的代码:
value attribute for p:fileDownload should be an instance of StreamedContent. check in the documentation here on page 174 it should be something like code below:
<p:commandButton id="downloadLink" value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)"
icon="ui-icon-arrowthichk-s">
<p:fileDownload value="#{fileDownloadController.file}" />
</p:commandButton>
private StreamedContent file;
public StreamedContent getFile() {
return file;
}
public void setFile(StreamedContent file){
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/images/optimusprime.jpg");
file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
}
如果要从 h:commandLink 下载PDF()侦听器,则应使用actionListener而不是action属性:
If you want downloadPDF() listener fired from h:commandLink you should use actionListener instead of action attribute:
<h:commandLink id="downloadLink" value="Download PDF" target="_blank" actionListener="#{pc_ArchiveFiles.downloadPDF}" />