【问题标题】:javax.el.PropertyNotFoundException:Property not found on type java.io.File in primefaces downloadjavax.el.PropertyNotFoundException:在 primefaces 下载中的 java.io.File 类型上找不到属性
【发布时间】:2012-09-03 19:58:56
【问题描述】:

我正在使用 Primefaces 3.2 并开发文件下载功能,我正在从本地获取文件名列表,我想在带有可点击选项的 jsf 数据表中显示它们(h:commandlink)。

当我执行我的代码时,我得到以下异常。

javax.el.PropertyNotFoundException: /faces/fileDownload.xhtml 在行 33 和第 115 列值 =“#{x.fileName}”:找不到属性“文件名” 在 java.io.File 类型上

我的代码看起来像这个 Java 文件

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@ManagedBean(name="fileDownloadController")
@SessionScoped
public class FileDownloadController {

    private StreamedContent file;
    private List<File>  listfiles=new ArrayList<File>();
    private String fileName;

    public FileDownloadController() {        
        File filestream=new File("C:/temp.pdf");
        InputStream stream=null;
        try {
            stream = new FileInputStream(filestream);
        file = new DefaultStreamedContent(stream, "application/pdf", "temp.pdf");
        stream.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public List<File> getListfiles() {
        File folder = new File("c:\\");
        File[] listOfFiles = folder.listFiles();
        listfiles=Arrays.asList(listOfFiles);
        int i;
        for(i=0;i<listfiles.size();i++){
       System.out.println("The List of file are"+listfiles.get(i));
       listfiles.get(i);
        }
        return listfiles;
    }

    public void setListfiles(List<File> listfiles) {
        this.listfiles = listfiles;
    }

    public String getFileName() {
        getListfiles();
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public StreamedContent getFile() {
        return this. file;
    }
}

我的 XHTML 看起来像这样。

<h:form id="form">  
 <h:dataTable value="#{fileDownloadController.listfiles}" var="x" 
              bgcolor="#F1F1F1" border="10" cellpadding="5" 
              cellspacing="3" first="0" rows="4" width="50%" 
              summary="This is a JSF code to create dataTable.">
              <h:column>
                <f:facet name="header">
                <h:outputText value="File Names"></h:outputText>
                </f:facet>
                <h:commandLink value="#{x.fileName}" onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)">
                        <p:fileDownload value="#{fileDownloadController.file}" />
                </h:commandLink>
             </h:column>
               </h:dataTable> 
</h:form>  

我不知道我哪里错了。请帮助我。

【问题讨论】:

标签: jsf-2 primefaces download myfaces


【解决方案1】:

您是如何使用#{x.fileName} 的?仔细查看the javadoc of the java.io.File class。对,没有像getFileName() 这样的方法。这正是异常试图告诉你的。

value="#{x.fileName}":在 java.io.File 类型上找不到属性“fileName”

您很可能打算改用getName() 方法。

#{x.name}

与具体问题无关,如果您使用 var="file" 而不是无意义的 var="x",您的代码会更加自文档化。

【讨论】:

  • @BalusC .........我尝试使用文件名以外的其他名称我没有工作。神奇的是你的回答它正在工作。你摇滚人......我是大粉丝你和你的答案......
  • @BalusC..my 文件名将如上表所示。当用户单击文件时,我想将该文件名传递给 FileDownloadController() 方法。我在 。它不起作用。当我将参数传递给 filedownloadController(string name) 时,我收到 InstantationException。如果我更改方法名称,它不起作用。请在此处提供帮助。
  • 这是一个不同的问题。按右上角的 按钮询问相关问题。
  • 嘿,我在stackoverflow.com/questions/12379416/… 的单独问题中更新了上述内容。你能看看那个问题吗?提前谢谢
猜你喜欢
  • 2015-10-09
  • 2017-09-01
  • 2012-05-03
  • 2014-10-20
  • 2014-10-31
  • 2013-04-18
相关资源
最近更新 更多