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