I have a fileDownload component inside a dataTable but when I click it it seems the filedownloader is called before the datlis.filepath variable is set by setPropertyActionListener.
When I click download I get “Cant instantiate class: ui.FileDownloader.com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: ui.FileDownloader.”
My jsf code is:
<p:column headerText="Metadata" style="width:40px">
<p:commandButton id="selectButton" rendered="#{datlis.has_metadata}" icon="ui-icon-circle-arrow-s" title="View" ajax="false" >
<f:setPropertyActionListener value="#{datlis.filepath}" target="#{filedownloader.filepath}" />
<p:fileDownload value="#{filedownloader.file}" />
</p:commandButton>
</p:column>
The bean that ‘datlis’ represents is ViewScoped in my application – and I’ve checked that datlist.filepath is not null. And the file download bean (FileDownloader) is as follows:
@ManagedBean(name="filedownloader")
@RequestScoped
public class FileDownloader {
private StreamedContent file;
public StreamedContent getFile() {
return file;
}
@ManagedProperty(value="#{param.filepath}")
private String filepath;
public String getFilepath() {
return filepath;
}
public void setFilepath(String filepath) {
System.out.println("> "+filepath);
this.filepath = filepath;
System.out.println(">> "+this.filepath);
}
public FileDownloader() throws FileNotFoundException {
System.out.println("100");
String filename = "/opt/glassfish3/glassfish/domains/domain1/datasets/string_compare/Business v2 Metadata/README.txt";
InputStream stream = new FileInputStream(filepath);
file = new DefaultStreamedContent(stream, "text/txt", "README.txt");
}
The stack trace mentions a nullpointer exception regarding the inputstream and that’s why I’m thinking the ‘filepath’ variable isn’t being set – plus my system output only shows the “100” from the System.out.println, and no System outputs from the setFilepath function…as if it’s not called at all.
I’ve also tried:
<p:column headerText="Metadata" style="width:40px">
<p:commandButton id="selectButton" rendered="#{datlis.has_metadata}" icon="ui-icon-circle-arrow-s" title="View" ajax="false" >
<f:param name="filepath" value="#{datlis.filepath}" />
<p:fileDownload value="#{filedownloader.file}" />
</p:commandButton>
</p:column>
with the added code just above the filepath getter/setter in my FileDownloader class:
@ManagedProperty(value="#{param.filepath}")
private String filepath;
But this also doesn’t seem to work. Any ideas? I feel I’m on the right track perhaps just misusing the elements…
Managed properties will be injected after construction. So you will get an NPE if you try to access them in the bean’s constructor.
Use a method annotated with
@PostConstruct. It will automatically be invoked after construction and property injection: