【问题标题】:Downloading file from IceFaces tree从 IceFaces 树下载文件
【发布时间】:2012-02-12 20:20:06
【问题描述】:

我还在学习JSF的路上。我有一个带有 IceFaces commandLink 的 IceFaces 树来尝试下载文件。到目前为止,这是我的 xhtml 和我的支持 bean。当我单击 commandLink 时,它只打印两条消息,然后它什么也不做,并且根本不显示任何警告任何错误......如何知道发生了什么?我错过了什么?

干杯

XHTML

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ice="http://www.icesoft.com/icefaces/component">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <ice:tree id="tree"
                      value="#{treeBean.model}"
                      var="item"
                      hideNavigation="false"
                      hideRootNode="false"
                      imageDir="./images/">
                <ice:treeNode>
                    <f:facet name="icon">
                        <ice:panelGroup style="display: inline">
                            <h:graphicImage value="#{item.userObject.icon}"/>
                        </ice:panelGroup>
                    </f:facet>
                    <f:facet name="content">
                        <ice:panelGroup style="display: inline">
                            <ice:commandLink action="#{treeBean.doDownload(item.userObject.fileAbsolutePath)}">
                                <ice:outputText value="#{item.userObject.text}"/>
                            </ice:commandLink>
                        </ice:panelGroup>
                    </f:facet>
                </ice:treeNode>
            </ice:tree>
        </h:form>
    </h:body>
</html>

@ManagedBean
@ViewScoped
public class TreeBean implements Serializable {

    private final DefaultTreeModel model;

    /** Creates a new instance of TreeBean */
    public TreeBean() {

// create root node with its children expanded
        DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
        IceUserObject rootObject = new IceUserObject(rootTreeNode);
        rootObject.setText("Root Node");
        rootObject.setExpanded(true);
        rootObject.setBranchContractedIcon("./images/tree_folder_close.gif");
        rootObject.setBranchExpandedIcon("./images/tree_folder_open.gif");
        rootObject.setLeafIcon("./images/tree_document.gif");
        rootTreeNode.setUserObject(rootObject);

        // model is accessed by by the ice:tree component via a getter method
        model = new DefaultTreeModel(rootTreeNode);

        // add some child nodes
        for (int i = 0; i < 3; i++) {
            DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
            FileSourceUserObject branchObject = new FileSourceUserObject(branchNode);
            branchObject.setText("SteveJobs.jpg");
            branchObject.setFileAbsolutePath("/Users/BRabbit/Downloads/SteveJobs.jpg");
            branchObject.setBranchContractedIcon("./images/tree_folder_close.gif");
            branchObject.setBranchExpandedIcon("./images/tree_folder_open.gif");
            branchObject.setLeafIcon("./images/tree_document.gif");
            branchObject.setLeaf(true);
            branchNode.setUserObject(branchObject);
            rootTreeNode.add(branchNode);
        }


    }

    public DefaultTreeModel getModel() {
        return model;
    }

    public void doDownload(String fileAbsolutePath) {
        System.out.println(fileAbsolutePath);

        File file = new File(fileAbsolutePath);

        if(file.exists())
            System.out.println("Yes"); //It exists !

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();

        externalContext.setResponseHeader("Content-Type", externalContext.getMimeType(file.getName()));
        externalContext.setResponseHeader("Content-Length", String.valueOf(file.length()));
        externalContext.setResponseHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");

        InputStream input = null;
        OutputStream output = null;

        try {
            input = new FileInputStream(file);
            output = externalContext.getResponseOutputStream();
            IOUtils.copy(input, output);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TreeBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TreeBean.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            IOUtils.closeQuietly(output);
            IOUtils.closeQuietly(input);
        }

        facesContext.responseComplete();

    }
}

Bean 的对象

public class FileSourceUserObject extends IceUserObject{

    String fileAbsolutePath;

    public FileSourceUserObject(DefaultMutableTreeNode wrapper) {
        super(wrapper);
    }

    public String getFileAbsolutePath(){
        return fileAbsolutePath;
    }

    public void setFileAbsolutePath(String fileAbsolutePath){
        this.fileAbsolutePath = fileAbsolutePath;
    }

}

【问题讨论】:

    标签: jsf download icefaces


    【解决方案1】:

    我终于做到了! 下面是一些代码,允许用户查看(文件)树并下载它们。

    XHTML

            <ice:tree id="tree"
                      value="#{treeBean.model}"
                      var="item"
                      hideNavigation="false"
                      hideRootNode="false"
                      imageDir="./images/">
                <ice:treeNode>
                    <f:facet name="icon">
                        <ice:panelGroup style="display: inline">
                            <h:graphicImage value="#{item.userObject.icon}"/>
                        </ice:panelGroup>
                    </f:facet>
                    <f:facet name="content">
                        <ice:panelGroup style="display: inline-block">
                            <ice:outputResource resource="#{item.userObject.resource}" 
                                                fileName="#{item.userObject.text}"
                                                shared="false"/>
                        </ice:panelGroup>
                    </f:facet>
                </ice:treeNode>
            </ice:tree>
    

    支持豆

    @ManagedBean
    @ViewScoped
    public class TreeBean implements Serializable {
    
        private final DefaultTreeModel model;
    
        public TreeBean() {
    
            DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
            FileSourceUserObject rootObject = new FileSourceUserObject(rootTreeNode);
            rootObject.setText("Root Node");
            rootObject.setExpanded(true);
            rootObject.setBranchContractedIcon("./images/tree_folder_close.gif");
            rootObject.setBranchExpandedIcon("./images/tree_folder_open.gif");
            rootTreeNode.setUserObject(rootObject);
    
            // model is accessed by by the ice:tree component via a getter method
            model = new DefaultTreeModel(rootTreeNode);
            ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
            // add some child nodes
            for (int i = 0; i < 3; i++) {
                DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
                FileSourceUserObject branchObject = new FileSourceUserObject(branchNode);
                branchObject.setText("Test.jpg");
                branchObject.setResource(new SRCResource("/<filePath>/Test.jpg"));
                branchObject.setBranchContractedIcon("./images/tree_folder_close.gif");
                branchObject.setBranchExpandedIcon("./images/tree_folder_open.gif");
                branchObject.setLeafIcon("./images/tree_document.gif");
                branchObject.setLeaf(true);
                branchNode.setUserObject(branchObject);
                rootTreeNode.add(branchNode);
            }
    
    
        }
    
        public DefaultTreeModel getModel() {
            return model;
        }
    

    }

    SRCResource

    public class SRCResource implements Resource {
    
        private String fileAbsolutePath;
        private final Date lastModified;
    
        public SRCResource(String fileAbsolutePath) {
            this.fileAbsolutePath = fileAbsolutePath;
            this.lastModified = new Date();
        }
    
        @Override
        public String calculateDigest() {
            return "No lo calcularé jamás !!";
        }
    
        @Override
        public InputStream open() throws IOException {
            return (InputStream)(new FileInputStream(fileAbsolutePath));
        }
    
        @Override
        public Date lastModified() {
            return lastModified;
        }
    
        @Override
        public void withOptions(Options optns) throws IOException {
        }
    
        public String getFileAbsolutePath() {
            return fileAbsolutePath;
        }
    
        public void setFileAbsolutePath(String fileAbsolutePath) {
            this.fileAbsolutePath = fileAbsolutePath;
        }
    
    }
    

    Web.xml

    添加以下内容

    <servlet>
        <servlet-name>Resource Servlet</servlet-name>
        <servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/xmlhttp/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/icefaces/*</url-pattern>
    </servlet-mapping>
    

    就是这样!只需根据您的需要调整代码! 干杯!

    更多信息http://wiki.icefaces.org/display/ICE/Adding+ICEfaces+to+Your+Application

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多