SRy
SRy

Reputation: 2967

javax.el.PropertyNotFoundException:Property not found on type java.io.File in primefaces download

I am using Primefaces 3.2 and developing the file download functionality and I am getting list of file names from my local which i wanted to display them in jsf datatable with clickable option(h:commandlink).

When I excute my Code I am getting following exception.

javax.el.PropertyNotFoundException: /faces/fileDownload.xhtml at line 33 and column 115 value="#{x.fileName}": Property 'fileName' not found on type java.io.File

My Code looks like this Java File

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;
    }
}

My XHTML looks like this.

<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>  

I am not able to figure out where i went Wrong.Please help me.

Upvotes: 2

Views: 5164

Answers (1)

BalusC
BalusC

Reputation: 1108632

How did you come to using #{x.fileName}? Look carefully in the javadoc of the java.io.File class. Right, there's no such method like getFileName(). That's exactly what the exception is trying to tell you.

value="#{x.fileName}": Property 'fileName' not found on type java.io.File

Most likely you meant to use the getName() method instead.

#{x.name}

Unrelated to the concrete problem, your code would be more self-documenting if you used var="file" instead of the nonsensicial var="x".

Upvotes: 1

Related Questions