2011-11-08 79 views
1

我有一棵樹(tree2 tomahawk 1.1.11),它顯示目錄和文件的列表。當我點擊一個文件時,我想顯示下載對話框讓客戶端下載文件。我的頁面看起來像使用commandLink和Tree2 tomahawk顯示/下載txt或csv文件JSF

... 
    <h:form> 
     <t:tree2 id="tree" value="#{listFiles.treeData}" 
       var="node" varNodeToggler="t" > 
      <f:facet name="folder"> 
       <h:panelGroup> 
        <f:facet name="expand"> 
         <t:graphicImage value="images/folderOpen.png" 
             rendered="#{t.nodeExpanded}}" 
             border="0" /> 
        </f:facet> 
        <f:facet name="collapse"> 
         <t:graphicImage value="images/folderClose.png" 
             rendere="#{t.nodeExpanded}}" 
             border="0" /> 
        </f:facet> 
        <h:outputText value="#{node.description}" 
            styleClass="nodeFolder" /> 
       </h:panelGroup> 
      </f:facet> 
      <f:facet name="file"> 
       <h:panelGroup> 
        <h:commandLink action="#{listFiles.download()}" > 
         <t:graphicImage value="images/file.png" border="0" /> 
         <h:outputText value="#{node.description}" /> 
        </h:commandLink> 
       </h:panelGroup> 
      </f:facet> 
     </t:tree2> 
    </h:form> 
    ... 

我的bean是

@ManagedBean 
@RequestScoped 
public class ListFiles implements Serializable { 

    private String path = "C:\\"; 
    private TreeNode treeRoot; 
    private File dirRoot; 
    @ManagedProperty("#{userVerifier}") 
    private UserVerifier userVerifier; 

    public void setUserVerifier(UserVerifier userVerifier) { 
     this.userVerifier = userVerifier; 
    } 

    public UserVerifier getUserVerifier() { 
     return userVerifier; 
    } 

    public TreeNode getTreeData() { 
     path = loadConfiguredPath(); 
     String dependencia = userVerifier.getDependencia(); 

     if (dependencia.equals("DESARROLLO")) { 
      path = path + "dataFiles"; 
      treeRoot = new TreeNodeBase("folder", "SRC", false); 
     } else { 
      path = path + "dataFiles\\" + dependencia; 
      treeRoot = new TreeNodeBase("folder", dependencia, false); 
     } 

     dirRoot = new File(path); 
     createTree(dirRoot, treeRoot); 

     return treeRoot; 
    } 

    private void createTree(File fileRoot, TreeNode treeRoot) { 
     File[] files = fileRoot.listFiles(); 
     TreeNodeBase tnb; 
     for (File f : files) { 
      if (f.isDirectory()) { 
       tnb = new TreeNodeBase("folder", f.getName(), false); 
       treeRoot.getChildren().add(tnb); 
       createTree(f, tnb); 
      } 
      if (f.isFile()) { 
       tnb = new TreeNodeBase("file", f.getName(), false); 
       treeRoot.getChildren().add(tnb); 
      } 
     } 
     return; 
    } 

    private String loadConfiguredPath() { 
     String dir; 
     ReadXML reader = new ReadXML(".\\webapps\\SRC\\configFiles\\confSRC.xml"); 
     dir = reader.getValue("baseDir"); 
     if (dir == null) { 
      return path; 
     } else { 
      return dir; 
     } 
    } 

    public String download(){ 
     System.out.println("Yes we are downloading"); 
     return "ok"; 
    } 
} 

一切正常,只是我不知道如何實現下載操作時,單擊H:commandLink

我擁有的唯一文件類型是txt或csv。

UPDATE:

現在,當我的代碼,這將引發異常。

javax.servlet.ServletException 
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:422) 
    org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349) 

UPDATE:

我會後我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 

    <welcome-file-list> 
     <welcome-file>faces/index.xhtml</welcome-file> 
    </welcome-file-list> 

    <filter> 
     <filter-name>MyFacesExtensionsFilter</filter-name> 
     <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class> 

     <init-param> 
      <param-name>uploadMaxFileSize</param-name> 
      <param-value>5g</param-value> 
     </init-param> 
     <init-param> 
      <param-name>uploadThresholdSize</param-name> 
      <param-value>500m</param-value> 
     </init-param> 
    </filter> 

    <filter-mapping> 
     <filter-name>MyFacesExtensionsFilter</filter-name> 
     <servlet-name>Faces Servlet</servlet-name> 
    </filter-mapping> 

    <filter-mapping> 
     <filter-name>MyFacesExtensionsFilter</filter-name> 
     <servlet-name>/faces/*</servlet-name> 
    </filter-mapping> 

    <context-param> 
     <param-name>org.apache.myfaces.CHECK_EXTENSIONS_FILTER</param-name> 
     <param-value>false</param-value> 
    </context-param> 

</web-app> 

回答

1

基本上,你需要通過周圍的物理File或至少File#getAbsolutePath()的值,這樣下載操作方法可以從磁盤讀取它。我從來沒有使用<t:tree2>,所以我檢查了TreeNodeBase的Javadoc,並且它似乎不支持任何其他節點值作爲String description。無法使用File進行設置。所以你真的需要通過File#getAbsolutePath()。我想你可以使用這個String identifier說法:

tnb = new TreeNodeBase("file", f.getName(), f.getAbsolutePath(), false); 

然後,在視圖中,只需將它傳遞給操作方法:

<h:commandLink action="#{listFiles.download(node.identifier)}" > 

最後,如下流呢:

public String download(String absolutePath) throws IOException { 
    File file = new File(absolutePath); 
    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); 
    } finally { 
     IOUtils.closeQuietly(output); 
     IOUtils.closeQuietly(input); 
    } 

    facesContext.responseComplete(); 
} 
+0

'javax.servlet.ServletException \t javax.faces.webapp.FacesServlet.service(FacesServlet.java:422) \t org.apache .myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349)'仍然有這個異常。我認爲它與web.xml上的ExtensionFilter配置有關 – BRabbit27

+0

這不太可能。根源是什麼?再往下追溯到蹤跡。 – BalusC

+0

顯示java.lang.NullPointerException \t javax.faces.component.UIComponentBase $ FacetsMap.put(UIComponentBase.java:2923) \t javax.faces.component.UIComponentBase $ FacetsMap.put(UIComponentBase.java:2895) \t融爲一體。 sun.faces.application.view.StateManagementStrategyImpl $ 4.visit(StateManagementStrategyImpl.java:344) \t com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151) \t javax.faces.component.UIComponent .visitTree(UIComponent.java:1589) \t javax.faces.component.UIComponent.visitTree(UIComponent.java:1600) \t javax.faces.component.UIForm.v isitTree(UIForm。java:344) – BRabbit27