使用 jsf 页面中的命令按钮下载文件。使用: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
I used
<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 内的第二个按钮来下载文件。
我为此搜索了很多,似乎是一个富人面孔的错误。