【问题标题】:Download button is not working in p:dataTable下载按钮在 p:dataTable 中不起作用
【发布时间】:2015-12-31 06:14:04
【问题描述】:

我有一个生成一些记录的表单,如下图--> prime faces datatable image

每一行都有一个下载按钮,它会生成一个唯一的文件。 我想从下载按钮中的每一行数据中下载一个文件。

当我点击第一行的下载按钮时,下载了一个文件。接下来,我单击了第二行下载按钮,但它再次下载了第一行文件。

我为每一行的下载按钮获取相同的文件。

我的代码:

xhtml 页面:

<p:column>
  <f:facet name="header">
    <h:outputText value="Download" />
  </f:facet>
  <p:commandButton  value="Download" ajax="false" actionListener="#   {sBean.prepDownload}">
    <p:fileDownload value="#{sBean.download}"/>
  </p:commandButton>
</p:column>

Bean 类:

StudentReportPojo info = null;
if (list != null && list.size() > 0) {

    for(StudentReportPojo aa:list){
        filepath=aa.getPath();
        System.out.println("file path of file "+filepath);
        //prepDownload(filepath);
    }

}
private  DefaultStreamedContent download;

public void setDownload(DefaultStreamedContent download) {
    this.download = download;
}

public DefaultStreamedContent getDownload() throws Exception {
    System.out.println("GET = " + download.getName());
    return download;
}

public void prepDownload() throws Exception {

        File file = new File(filepath);
        FileInputStream input = new FileInputStream(file);
        ExternalContext externalContext = FacesContext.getCurrentInstance()
                        .getExternalContext();
        setDownload(new DefaultStreamedContent(input,
                        externalContext.getMimeType(file.getName()), file.getName()));
        System.out.println("PREP = " + download.getName());

}

请提前给我您的建议和感谢。

【问题讨论】:

  • 您应该花点时间正确地格式化您的代码。它使我们更容易阅读
  • 我在 prepDownload actionListener 或 getDownload 中看到 nothing 相信您有任何逻辑可以在每一行中下载不同的文件。提示:将 something 传递给 action 或 actionListener 或 &lt;p:fileDownload&gt; 的值属性,以便可以下载另一个文件。例如。将 filepath 传递给 ActionListener,如 #{sBean.prepDownload(datatableVarAttribute)}

标签: jsf primefaces


【解决方案1】:

我在您的代码中看到您没有将特定行的实例路径传递给prepDownload() 方法,因此您需要修改数据表,例如将路径值传递给prepDownload(dt.path) 方法。我假设这里 dt 是数据表变量,路径是变量,它存储在您的数据表中为getList()

  <p:dataTable var="dt" value="#{managedBean.getList()}">
       .
       //put your rest code...
       .
       <p:column>
          <f:facet name="header">
            <h:outputText value="Download" />
          </f:facet>
          <p:commandButton value="Download" ajax="false" actionListener="#{sBean.prepDownload(dt.path)}">
              <p:fileDownload value="#{sBean.download}"/>
          </p:commandButton>
      </p:column>
  </p:dataTable>

并且您需要在 bean 类中将 prepDownload() method 更改为 prepDownload(String instancepath),例如:

public void prepDownload(String instancepath) throws Exception {    
        File file = new File(instancepath); //here passing instance path
        .
        .
        //rest of code need to put same...
        .
        . 
}

我试过了,它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2013-08-09
    • 2013-11-20
    相关资源
    最近更新 更多