2010-12-02 29 views
7

在jsf頁面中使用commandButton來下載文件。使用:JSF & Richfaces。在jsf頁面中使用commandButton來下載文件

我有一個表(延伸ExtendedDataModel實現可修改,可序列化)與一些數據和在每一行中的按鈕「下載」。

<a4j:commandButton id="getDownload" value="download" 
    style="margin-left:10px;margin-right:10px;width:100px;" 
    action="#{controller.download}" immediate="true" ajaxSingle="true"> 
    <f:setPropertyActionListener target="#{controller.idString}" value="#{item.id}" />      
</a4j:commandButton> 

我必須建立在文件中的控制器:

public void download(){ 
OutputStream out = null; 
.... 

FacesContext fc = FacesContext.getCurrentInstance(); 
HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse(); 
out = response.getOutputStream(); 

ZipOutputStream zipout = new ZipOutputStream(out); 
..... 

zipout.close(); 
response.setContentType("application/octet-stream"); 
response.addHeader("Content-Disposition", "attachment; filename=\""+filename+"\""); 
out.flush(); 
.... 

} finally { 
    try { 
     if (out!=null){ 
      out.close(); 
     } 
     FacesContext.getCurrentInstance().responseComplete(); 
    } catch (IOException e) { 
     logger.error(e); 
    } 

} 
... 
} 

的問題開始,當我實現了我的ExtendedDataModel自我。 起初我使用h:commandLink,但控制器方法從來沒有被調用過......我試過並嘗試過......現在調用了正確的方法,但(zip)文件內容顯示在頁面中。我想在頁面中有一個按鈕/鏈接,用戶可以點擊該鏈接下載文件。頁面本身不應該改變。有任何想法嗎?

我可以創建一個servlet,但我不明白爲什麼ExtendedDataModel改變了內部鏈接的行爲。

EDIT1

我以前用過

<h:commandLink id="getDownload" value="download" action="#{controller.download}">              
          <f:setPropertyActionListener target="#{controller.idString}" value="#{item.id}" />        
         </h:commandLink> 

。它適用於「普通」richfaces表,但不是當我在擴展ExtendedDataModel的自己的表中使用它時。

編輯2 - 解決方案/解決方法

這是不可能使用h:的commandButton ..鏈接... ...不管是自制表內,下載文件。我現在使用表格中的一個按鈕來渲染一個新的PanelGroup和新的PanelGroupt中的第二個按鈕來下載文件。 我爲此搜索了很多,看起來像一個豐富的面孔的bug。

回答

10

您無法通過ajax請求下載文件。用h:commandButton代替a4j:commandButton


更新按您的問題更新:獲得命令鏈接/一UIData組件內的按鈕,如<h:dataTable><rich:dataTable>等工作,你需要確保它保持數據模型豆(無論是在UIData組件的value屬性後面)是否在請求表單提交期間保留完全相同的數據模型。如果您想保留bean請求的範圍,那麼最簡單的方法是在<a4j:keepAlive>標記中引用bean。

+0

H:的commandButton不工作,因爲我延長了ExtendedDataModel。我使用了「普通」richfaces表 - h:commandButton像魅力一樣工作。然後我用自己的表替換了表:h:commandButton不再調用該方法..它只是刷新了網站。類似於site.jsf# – 2010-12-02 14:31:06

4
<h:commandButton id="getDownload" value="download" action="#{controller.download()}" > 

</h:commandButton> 
public class Controller { 

OutputStream out = null; 
String filename = "ali.pdf"; 
public void download() throws IOException { 



    FacesContext fc = FacesContext.getCurrentInstance(); 
    HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse(); 
    out = response.getOutputStream(); 

    ZipOutputStream zipout = new ZipOutputStream(out); 




    response.setContentType("application/octet-stream"); 
    response.addHeader("Content-Disposition", "attachment; filename=\""+filename+"\""); 
    out.flush(); 







try { 
     if (out != null) { 
      out.close(); 
     } 
     FacesContext.getCurrentInstance().responseComplete(); 
    } catch (IOException e) { 

    } 

}