簡體   English   中英

在jsf頁面中使用commandButton下載文件

[英]Using a commandButton in a jsf Page to download a file

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

我有一個表(擴展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,.. Link ...在自制表的內部,下載文件。 我現在使用表中的一個按鈕來渲染新的PanelGroup和新PanelGroupt內的第二個按鈕來下載文件。 我為此搜索了很多,似乎是一個富有面孔的bug。

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


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

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

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM