我在dataTable中有一个fileDownload组件但是当我单击它时,似乎在netlis.filepath变量由setPropertyActionListener设置之前调用了filedownloader。
当我点击下载时,我得到“Cant instantiate class:ui.FileDownloader.com.sun.faces.mgbean.ManagedBeanCreationException:Cant instantiate class:ui.FileDownloader。”
我的jsf代码是:
<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>
'datlis'代表的bean是我的应用程序中的ViewScoped - 我检查过datlist.filepath不为null。文件下载bean(FileDownloader)如下:
@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");
}
堆栈跟踪提到了关于输入流的nullpointer异常,这就是为什么我认为'filepath'变量没有设置 - 加上我的系统输出只显示System.out.println中的“100”,并且没有来自setFilepath函数的系统输出......好像它根本没有被调用。
我也试过了:
<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>
在FileDownloader类的filepath getter / setter上面添加了代码:
@ManagedProperty(value="#{param.filepath}")
private String filepath;
但这似乎也不起作用。有任何想法吗?我觉得我走在正确的轨道上或许只是误用了元素......
答案 0 :(得分:1)
管理后的物业将在施工后注入。因此,如果您尝试在bean的构造函数中访问它们,您将获得一个NPE。
使用注释为@PostConstruct
的方法。它将在构造和属性注入后自动调用:
@PostConstruct
public void init() {
// do your initializations here
}