【问题标题】:How to render file's download button only if the file is available using primefaces 5.3仅当文件使用 primefaces 5.3 可用时如何呈现文件的下载按钮
【发布时间】: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


【解决方案1】:

我看到两个选项:

  1. controller.fileList() 提供可供下载的文件 => 没有机会下载不存在的文件
  2. controller.fileList() 提供文件列表,包括未准备好下载的文件 => 使用 commandButton 的渲染或禁用属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-18
    • 2020-08-25
    • 2017-03-05
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    相关资源
    最近更新 更多