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
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 theselectedTreeNodeto 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
setSelectedTreeNodemethod to make sure the setter is being invoked when you click the tree node. Use thegetData()method on theTreeNodeto 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.