【发布时间】:2016-05-23 18:34:12
【问题描述】:
Java EE 7 -- Primefaces 5.3
我正在上传文件列表,然后返回另一个页面,该页面为用户提供下载这些文件的访问权限。当用户点击下载按钮时,我将 fileName 作为属性传递回我的控制器,然后返回要下载的文件。如果文件可用,我正在寻找一种呈现下载按钮的方法。那可能吗?如果有怎么办?
这里是download.xhtml中的文件数据表:
<p:dataTable var="file" value="#{controller.fileList}">
<p:column headerText="File Name">
<p:outputLabel value="#{file.fileName}"/>
</p:column>
<p:column headerText="download">
<p:commandButton value="Download" id="download" onclick="PrimeFaces.monitorDownload(start, stop);" ajax="false" actionListener="#{controller.prepareToDownload}" icon="ui-icon-arrowthick-1-s">
<f:attribute name="fileName" value="#{file.fileName}" />
<p:fileDownload value="#{controller.download}" />
</p:commandButton>
</p:column>
控制器:
public void prepareToDownload(ActionEvent actionEvent) throws FileNotFoundException {
String directory = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("uploadDirectory");
fileName = (String)actionEvent.getComponent().getAttributes().get("fileName");
file = new File(directory, fileName);
InputStream input = new FileInputStream(file);
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
download = new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName());
}
public void setDownload(DefaultStreamedContent download) {
this.download = download;
}
public DefaultStreamedContent getDownload() throws Exception {
return download;
}
【问题讨论】:
-
使用渲染属性?
标签: jsf jsf-2 primefaces