如何使用 primefaces 树下载文件

发布于 2024-11-28 00:40:47 字数 1752 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-12-05 00:40:47

您是否尝试过将 一起使用?

<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) {
    }
}

最后一部分是将特定文件与特定树节点相关联。您可以使用 属性 selectionMode="single"selection="#{myBean.selectedTreeNode}"。用户选择一个树节点,这将导致在您的 bean 上设置(通过 setter 方法)selectedTreeNode

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。使用 TreeNode 上的 getData() 方法来获取您在创建树时最初放入其中的数据值。 getFileStreamedContent() 方法将使用该值来传递用户通过单击树节点选择的正确文件。

Have you tried using a <p:commandButton> with <p:fileDownload>?

<p:commandButton
        value="Download"
        title="Download"
        image="ui-icon-arrowthick-1-s"
        ajax="false">
    <p:fileDownload value="#{myBean.fileStreamedContent}" />
</p:commandButton>

In your backing bean (for example purposes, assuming your files are JPEGs):

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) {
    }
}

The last part is associating a particular file with a particular tree node. You can use the <p:tree> attributes selectionMode="single" and selection="#{myBean.selectedTreeNode}". The user selects a tree node and this will cause the selectedTreeNode to be set (via a setter method) on your bean.

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...
    }
}

In the getFileStreamedContent() method, just use the filename stored in your tree node object as the parameter to the FileInputStream() 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:

<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...
}

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 the getData() method on the TreeNode to get back the data value that you originally put in it when you created the tree. The getFileStreamedContent() method will use that value to deliver the correct file that the user selected by clicking on a tree node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文