如何使用 primefaces 树下载文件
我使用 primefaces 3.0.M2 和支持 bean 来添加排列在文件夹(模块)和子文件夹(作业)中的文件。我已经成功地做到了这一点,但我无法控制该文件以使其可下载。我想将该文件作为下载该特定文件的按钮,而不仅仅是普通文本。请检查下面的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上尝试过
I'm using primefaces 3.0.M2 with backing bean to add files arranged in folders (modules) and subfolders(assignments). I have managed to do that successfully, but I can't have control over the file to make it downloadable. I want to make the file as a button to download that specific file instead of just being a normal text. Please check the jsf code below:
<p:tree id="tree" value="#{files.root}" var="doc" selectionMode="single"
selection="#{files.selectedTreeNode}">
<p:treeNode>
<h:outputText value="#{doc}"/>
</p:treeNode>
</p:tree>
Here is my backing bean class:
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, browser: IE8 also tried on FireFox 3.6.12
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过将
与
一起使用?在您的支持 bean 中(出于示例目的,假设您的文件是 JPEG):
最后一部分是将特定文件与特定树节点相关联。您可以使用
属性selectionMode="single"
和selection="#{myBean.selectedTreeNode}"
。用户选择一个树节点,这将导致在您的 bean 上设置(通过 setter 方法)selectedTreeNode
。在
getFileStreamedContent()
方法中,只需使用存储在树节点对象中的文件名作为FileInputStream()
构造函数的参数。编辑
不要尝试在树中嵌入命令按钮,而是在页面上的某处提供一个命令按钮。当选择一个树节点时,它可以将关联的文件(要下载的)设置为 bean 的属性。让您的树如下所示:
在
setSelectedTreeNode
方法中放置 println 或日志记录语句,以确保单击树节点时调用 setter。使用TreeNode
上的getData()
方法来获取您在创建树时最初放入其中的数据值。getFileStreamedContent()
方法将使用该值来传递用户通过单击树节点选择的正确文件。Have you tried using a
<p:commandButton>
with<p:fileDownload>
?In your backing bean (for example purposes, assuming your files are JPEGs):
The last part is associating a particular file with a particular tree node. You can use the
<p:tree>
attributesselectionMode="single"
andselection="#{myBean.selectedTreeNode}"
. The user selects a tree node and this will cause theselectedTreeNode
to be set (via a setter method) on your bean.In the
getFileStreamedContent()
method, just use the filename stored in your tree node object as the parameter to theFileInputStream()
constructor.EDIT
Rather than try to embed command buttons in the tree, provide one command button somewhere on the page. When a tree node is selected it can set the associated file (to be downloaded) as a property on your bean. Make your tree look like this:
Put a println or logging statement in your
setSelectedTreeNode
method to make sure the setter is being invoked when you click the tree node. Use thegetData()
method on theTreeNode
to get back the data value that you originally put in it when you created the tree. ThegetFileStreamedContent()
method will use that value to deliver the correct file that the user selected by clicking on a tree node.