2012-08-24 46 views
-2

我需要幫助在JSF頁面中實現文件下載。到目前爲止,我設法創造這樣的:當我點擊表格行時如何下載文件

enter image description here

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"  
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:p="http://primefaces.org/ui"> 
    <h:head> 
     <ui:insert name="header">   
      <ui:include src="header.xhtml"/>   
     </ui:insert> 
    </h:head> 
    <h:body> 
     <h1><img src="resources/css/images/icon.png" alt="DX-57" /> Settings Center</h1> 
     <!-- layer for black background of the buttons --> 
     <div id="toolbar" style="margin: 0 auto; width:100%; height:30px; position:relative; background-color:black"> 
      <!-- Include page Navigation --> 
      <ui:insert name="Navigation">   
       <ui:include src="Navigation.xhtml"/>   
      </ui:insert> 
     </div> 
     <div id="logodiv" style="position:relative; top:35px; left:0px;"> 
      <h:graphicImage alt="Glassfish" style="position:relative" value="resources/images/logo_glassfish_settings.png" /> 
     </div> 
     <div id="main" style="margin: 0 auto; width:1190px; height:700px; position:absolute; background-color:transparent; top:105px"> 

      <div id="mainpage" style="margin: 0 auto; width:1190px; height:500px; position:absolute; background-color:transparent; top:80px"> 

       <div id="settingsdiv" style="width:350px; height:400px; position:absolute; background-color:r; top:20px; left:1px"> 

        <div id="settingsHashMap" style="width:1050px; height:400px; position:absolute; background-color:r; top:20px; left:1px"> 

         <h:form id="form" > 
          <p:growl id="growl" showDetail="true" sticky="true" /> 
          <!-- The sortable data table --> 
          <h:dataTable onmouseover="addOnclickToDatatableRows();" id="dataTable" headerClass="columnHeader" value="#{GlassfishController.dataList}" binding="#{table}" var="item"> 
           <!-- Check box --> 
           <h:column> 
            <f:facet name="header"> 
             <h:selectBooleanCheckbox style="margin-left: 0px;" value="#{GlassfishController.mainSelectAll}" class="checkall" > 
              <f:ajax listener="#{GlassfishController.selectAll}" render="@form" /> 
             </h:selectBooleanCheckbox> 
            </f:facet> 
            <h:selectBooleanCheckbox onclick="highlight(this)" value="#{GlassfishController.selectedIds[item.filename]}" > 
             <!-- if the user deselects one row deselect the main checkbox --> 
             <f:ajax listener="#{GlassfishController.deselectMain}" render="@form" /> 
            </h:selectBooleanCheckbox> 
            <!-- Click on table code --> 
            <h:outputLink id="lnkHidden" onclick="document.getElementById('form:btnHello').click(); return false;" style="text-decoration:none; color:white; background-color:black"> 
            </h:outputLink> 
           </h:column> 
           <!-- Row number --> 
           <h:column> 
            <f:facet name="header"> 
             <h:outputText value="№" />          
            </f:facet> 
            <h:outputText value="#{table.rowIndex + GlassfishController.firstRow + 1}" /> 
           </h:column>         
           <h:column> 
            <f:facet name="header"> 
             <h:commandLink value="File Name" actionListener="#{GlassfishController.sort}" style="text-decoration:none; color:white; background-color:black"> 
              <f:attribute name="sortField" value="filename" /> 
              <f:ajax render="@form" /> 
             </h:commandLink> 
            </f:facet> 
            <h:outputText value="#{item.filename}" /> 
           </h:column> 
           <h:column> 
            <f:facet name="header"> 
             <h:commandLink value="Size" actionListener="#{GlassfishController.sort}" style="text-decoration:none; color:white; background-color:black"> 
              <f:attribute name="sortField" value="size" /> 
              <f:ajax render="@form" /> 
             </h:commandLink> 
            </f:facet> 
            <h:outputText value="#{item.size} bytes" /> 
           </h:column> 

          </h:dataTable> 

          <!-- The paging buttons --> 
          <h:commandButton value="first" action="#{GlassfishController.pageFirst}" 
              disabled="#{GlassfishController.firstRow == 0}" > 
           <f:ajax render="@form" execute="@form"></f:ajax> 
          </h:commandButton>&nbsp; 


          <h:commandButton value="prev" action="#{GlassfishController.pagePrevious}" 
              disabled="#{GlassfishController.firstRow == 0}" > 
           <f:ajax render="@form" execute="@form"></f:ajax> 
          </h:commandButton>&nbsp; 

          <h:commandButton value="next" action="#{GlassfishController.pageNext}" 
              disabled="#{GlassfishController.firstRow + GlassfishController.rowsPerPage >= GlassfishController.totalRows}" > 
           <f:ajax render="@form" execute="@form"></f:ajax> 
          </h:commandButton>&nbsp;  

          <h:commandButton value="last" action="#{GlassfishController.pageLast}" 
              disabled="#{GlassfishController.firstRow + GlassfishController.rowsPerPage >= GlassfishController.totalRows}" > 
           <f:ajax render="@form" execute="@form"></f:ajax> 
          </h:commandButton>&nbsp; 

          <h:outputText value="Page #{GlassfishController.currentPage}/#{GlassfishController.totalPages}" /> 
          <br /> 

          <!-- The paging links --> 
          <ui:repeat value="#{GlassfishController.pages}" var="page"> 
           <h:commandLink value="#{page}" actionListener="#{GlassfishController.page}" 
               rendered="#{page != GlassfishController.currentPage}" style="text-decoration:none;color:white;"> 
            <f:ajax render="@form" execute="@form"></f:ajax> 
           </h:commandLink> 
           <h:outputText value="#{page}" escape="false" 
               rendered="#{page == GlassfishController.currentPage}" style="text-decoration:none;color:gray;"/> 
          </ui:repeat> 
          <br /> 

          <!-- Set rows per page --> 
          <h:outputLabel for="rowsPerPage" value="Rows per page" /> 
          <h:inputText id="rowsPerPage" value="#{GlassfishController.rowsPerPage}" size="3" maxlength="3" /> 
          <h:commandButton value="Set" action="#{GlassfishController.pageFirst}" > 
           <f:ajax render="@form" execute="@form"></f:ajax> 
          </h:commandButton>&nbsp; 
          <h:message for="rowsPerPage" errorStyle="color: red;" /> 

          <!-- hidden button --> 
          <h:commandButton id="deleterow" value="HiddenDelete" action="#{GlassfishController.deleteSelectedIDs}" style="display:none"> 
           <f:ajax render="@form"></f:ajax> 
          </h:commandButton> 

          <!-- the delete button --> 
          <h:button value="Delete" onclick="deletedialog(this, 'Do you want to delete the selected rows?'); return false;" /> 

          <script type="text/javascript" src="resources/js/tabs.js"></script> 


          <h:commandLink id="btnHello" value="Download Files" 
              action="#{GlassfishController.downloadFile}"> 
          </h:commandLink> 
         </h:form>      
        </div> 

       </div>      
      </div> 
     </div> 
    </h:body> 
</html> 


// Execute the download procedure 
    public void executeDownload(String filename) throws IOException{ 
     int DEFAULT_BUFFER_SIZE = 10240; 
     String filePath = "/opt/glassfish3/glassfish/domains/domain1/logs/" + filename; 

     FacesContext context = FacesContext.getCurrentInstance(); 
     HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); 
     File file = new File(filePath); 
     if (!file.exists()) 
     { 
      response.sendError(HttpServletResponse.SC_NOT_FOUND); 
      return; 
     } 
     response.reset(); 
     response.setBufferSize(DEFAULT_BUFFER_SIZE); 
     response.setContentType("text"); 
     response.setHeader("Content-Length", String.valueOf(file.length())); 
     response.setHeader("Content-Disposition", "attachment;filename=\"" 
       + file.getName() + "\""); 
     BufferedInputStream input = null; 
     BufferedOutputStream output = null; 
     try 
     { 
      input = new BufferedInputStream(new FileInputStream(file), 
        DEFAULT_BUFFER_SIZE); 
      output = new BufferedOutputStream(response.getOutputStream(), 
        DEFAULT_BUFFER_SIZE); 
      byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 
      int length; 
      while ((length = input.read(buffer)) > 0) 
      { 
       output.write(buffer, 0, length); 
      } 
     } 
     finally 
     { 
      input.close(); 
      output.close(); 
     } 
     context.responseComplete(); 
    } 

有,我解決不了,我需要幫助的問題。當我點擊下載按鈕時,我可以成功下載日誌文件。 我想單擊它時將一個文件下載到JSF表中。你能幫我實施嗎?或者你可以給我一些建議如何去做?總部設在您的意見

+0

這些日誌文件是否包含表格數據? – Vikdor

+0

這是打算標記爲JavaScript? –

+0

''中的按鈕使用ajax,並且無法使用ajax下載文件。 –

回答

2

,你可以使用一個commandLink /命令按鈕來調用另一個commandLink /的commandButton在同一頁。請記住,這些組件的行爲與普通的HTML組件相似。發佈此行爲

<h:form id="frmTest"> 
    <h:commandLink value="I will call the commandButton below me" 
     onclick="document.getElementById('frmTest:btnHello').click(); return false;" /> 
    <br /> 
    <h:commandButton id="btnHello" value="I'm the commandButton below the commandLink" 
     action="#{bean.someAction}" /> 
</h:form> 

編輯樣品:通過對這個問題的評論,你有幾種方法的參數傳遞給你的commandButton行動。我會告訴你一個使用JavaScript的人。邏輯很簡單:

  • 使用<h:inputHidden>標籤組件,該組件的值將是您要下載的文件的名稱。
  • 在發送onclick事件之前,請使用您需要的值填充此隱藏的組件。
  • 讓HTML請求繼續其工作。
<h:form id="frmTest"> 
    <h:commandLink id="hidFileName" value="#{bean.fileToDownload}" /> 
    <h:commandLink value="I will call the commandButton below me" 
     onclick="document.getElementById('frmTest:hidFileName').value='#{bean.fileName}'; document.getElementById('frmTest:btnHello').click(); return false;" /> 
    <br /> 
    <h:commandButton id="btnHello" value="I'm the commandButton below the commandLink" 
     action="#{bean.someAction}" /> 
</h:form> 
+0

我更新了代碼。有需要添加的小細節。當我點擊行來調用隱藏按鈕時,我需要傳遞文件名作爲參數,以告訴Java代碼要下載哪個文件。你會告訴我如何在你的例子中傳遞文件名作爲參數? – user1285928

+0

@ user1285928答案已更新。 –

相關問題