我正在使用带有辅助 bean 的 primefaces 3.0.M2 来添加排列在文件夹(模块)和子文件夹(分配)中的文件。我已经成功地做到了这一点,但我无法控制文件以使其可下载。我想将该文件作为一个按钮来下载该特定文件,而不仅仅是普通文本。请检查下面的jsf代码:
<p:tree id="tree" value="#{files.root}" var="doc" selectionMode="single"
selection="#{files.selectedTreeNode}">
<p:treeNode>
<h:outputText value="#{doc}"/>
</p:treeNode>
</p:tree>
这是我的支持 bean 类:
public class FilesBean implements Serializable {
private TreeNode root;
public TreeNode getRoot() {
root = new DefaultTreeNode("root", null);
TreeNode general = new DefaultTreeNode("General", root);
TreeNode module = null;
TreeNode assignment = null;
TreeNode fileNode = null;
if(getMoudles()!=null)
{
for(String s : getMoudles())
{
module = new DefaultTreeNode(s, root);
if(getAssignments()!=null)
{
for (Assignments as : getAssignments())
{
if(as.getMoudleid().equals(s))
assignment = new DefaultTreeNode(as.getAssignmentsPK().getAssignmentid(), module);
for(Files file : getFiles())
{
if (file.getFilesPK().getAssignmentid().equals(as.getAssignmentsPK().getAssignmentid()) && file.getThemodule().equals(s))
{fileNode = new DefaultTreeNode(file,assignment);}
}
}
}
}
}
return root;
}
PS: PrimeFaces 3.0.M2, JSF 2.0, J2EE 6 Web, Servlet 3.0, Glassfish 3.0, EJB 3.0, 浏览器: IE8 也试过FireFox 3.6.12
最佳答案
您是否尝试过使用 <p:commandButton>
与 <p:fileDownload>
?
<p:commandButton
value="Download"
title="Download"
image="ui-icon-arrowthick-1-s"
ajax="false">
<p:fileDownload value="#{myBean.fileStreamedContent}" />
</p:commandButton>
在您的支持 bean 中(出于示例目的,假设您的文件是 JPEG):
public StreamedContent getFileStreamedContent() {
try {
InputStream is = new BufferedInputStream(
new FileInputStream("/your/file/path/fileXYZ.jpg"));
return new DefaultStreamedContent(is, "image/jpeg", "fileXYZ.jpg");
} catch (FileNotFoundException e) {
}
}
最后一部分是将特定文件与特定树节点相关联。您可以使用 <p:tree>
属性 selectionMode="single"
和 selection="#{myBean.selectedTreeNode}"
.用户选择一个树节点,这将导致 selectedTreeNode
在你的 bean 上设置(通过 setter 方法)。
private TreeNode selectedTreeNode;
public void setSelectedTreeNode(TreeNode selectedTreeNode) {
this.selectedTreeNode = selectedTreeNode;
if (this.selectedTreeNode != null) {
Object yourTreeNodeData = this.selectedTreeNode.getData();
// do whatever you need to do with the data object...
}
}
在getFileStreamedContent()
方法,只需使用存储在树节点对象中的文件名作为 FileInputStream()
的参数构造函数。
编辑
与其尝试在树中嵌入命令按钮,不如在页面某处提供一个命令按钮。选择树节点后,它可以将关联文件(要下载)设置为 bean 的属性。让你的树看起来像这样:
<p:tree
value="#{myBean.rootTreeNode}"
var="node"
selectionMode="single"
selection="#{myBean.selectedTreeNode}">
<p:ajax event="select" listener="#{myBean.onNodeSelect}" />
<p:ajax event="unselect" listener="#{myBean.onNodeUnselect}" />
</p:tree>
public void onNodeSelect(NodeSelectEvent event) {
// put some logging here...
}
public void onNodeUnselect(NodeUnselectEvent event) {
// put some logging here...
}
在你的 setSelectedTreeNode
中放入一个 println 或日志语句方法以确保在单击树节点时调用 setter。使用 getData()
TreeNode
上的方法取回创建树时最初放入其中的数据值。 getFileStreamedContent()
方法将使用该值来传送用户通过单击树节点选择的正确文件。
关于java - 如何使用 primefaces 树下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913116/