我想在 primefaces 中下载一个名称可能不同的文件。
这是控制器的代码
@ManagedBean(name="fileDownloadController", eager = true)
@ViewScoped
public class FileDownloadController implements Serializable{
private StreamedContent file;
private String fileName;
public FileDownloadController() {
System.out.println("FileDownloadController sans arg");
System.out.println("getFileName:" + fileName);
InputStream stream = null;
try {
stream = new FileInputStream("D:/myFileDir/"+fileName);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileDownloadController.class.getName()).log(Level.SEVERE, null, ex);
}
file = new DefaultStreamedContent(stream, "image/jpg", fileName);
}
}
这是 xhtml
<c:forEach items="#{myBean.files}" var="file" >
<p:row>
<p:column>
<p:commandButton id="downloadLink" value="#{file.fileName}" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)" icon="ui-icon-arrowthichk-s" >
<f:setPropertyActionListener target="#{fileDownloadController.fileName}" value="#{file.fileName}"/>
<p:fileDownload value="#{fileDownloadController.file}" />
</p:commandButton>
</p:column>
</p:row>
</c:forEach>
问题是在控制器中 fileName 为 null,因此 f:setPropertyActionListener 未正确配置。
但是,我找不到解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)