2

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.

SRy
  • 2,901
  • 8
  • 36
  • 57
  • check this solution ; http://stackoverflow.com/questions/7822758/export-to-excel-jsf-and-primefaces/12526625#12526625 – newuserua Sep 24 '12 at 06:58

1 Answers1

1

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC.........I tried with other names otherthan fileName i didn't work.Magically with your answer it's working.You Rock Man....I am big fan of you and u r answers... – SRy Sep 11 '12 at 03:27
  • @BalusC..my filenames will as shown in above table.When User Clicks on the file I want to pass that file name to the FileDownloadController() method.I am passing it inside the .It's not working.When I pass parameter to filedownloadController(string name) I am getting InstantationException.If i change the method name it's not working. Please help here. – SRy Sep 11 '12 at 23:12
  • That's a different problem. Press `Ask Question` button on right top to ask a question about that. – BalusC Sep 11 '12 at 23:17
  • Hey I have updated above content in seperate question here http://stackoverflow.com/questions/12379416/issue-while-debugging-with-hdatatable-and-pdownload. Can you please take a look at that question.Thanks in advance – SRy Sep 12 '12 at 13:25