Java 在 jsf 页面中使用 commandButton 下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4335849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a commandButton in a jsf Page to download a file
提问by M.R.
Using a commandButton in a jsf Page to download a file. Using: JSF & Richfaces.
在 jsf 页面中使用 commandButton 下载文件。使用:JSF 和 Richfaces。
I have a table (extends ExtendedDataModel implements Modifiable, Serializable) with some data and in each row a button "download".
我有一个表(扩展 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>
I have to build the file in the controller:
我必须在控制器中构建文件:
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);
}
}
...
}
The Problem started, when I implemented the ExtendedDataModel my self. At first i used h:commandLink, but the controller method was never called... i tried and tried... now the correct method is called, but the (zip) file content is displayed in the page. I want a button/link in the page, which the user can click to download the file. The page itself should not change. Any ideas?
当我自己实现 ExtendedDataModel 时,问题就开始了。起初我使用了 h:commandLink,但控制器方法从未被调用过……我尝试并尝试过……现在调用了正确的方法,但页面中显示了(zip)文件内容。我想要页面中的按钮/链接,用户可以单击以下载文件。页面本身不应更改。有任何想法吗?
I can create a servlet, but i do not understand why the ExtendedDataModel changed the behavior of the links inside.
我可以创建一个 servlet,但我不明白为什么 ExtendedDataModel 改变了内部链接的行为。
Edit1
编辑1
I used
我用了
<h:commandLink id="getDownload" value="download" action="#{controller.download}">
<f:setPropertyActionListener target="#{controller.idString}" value="#{item.id}" />
</h:commandLink>
before. It works with the "normal" richfaces table, but not when i used it inside my own table which extends ExtendedDataModel.
前。它适用于“普通”richfaces 表,但当我在我自己的表中使用它时,它扩展了 ExtendedDataModel。
Edit 2 - Solution/Workaround
编辑 2 - 解决方案/解决方法
It is impossible to use the h:commandButton, .. Link... whatever inside of the self made table, to download a file. I am now using one button in the table to render a new PanelGroup and a second button inside of the new PanelGroupt to download the file. I googled a lot for this, seems like a rich faces bug.
不可能使用 h:commandButton, .. Link ... 无论在自制表中,下载文件。我现在使用表格中的一个按钮来呈现新的 PanelGroup,并使用新 PanelGroupt 中的第二个按钮来下载文件。我为此搜索了很多,似乎是一个富有的面孔错误。
采纳答案by BalusC
You can't download a file by ajax request. Replace a4j:commandButton
by h:commandButton
.
您无法通过 ajax 请求下载文件。替换a4j:commandButton
为h:commandButton
。
Updateas per your question update: to get command links/buttons inside an UIData
component such as <h:dataTable>
, <rich:dataTable>
, etc to work, you need to ensure that the bean which is holding the data model (whatever is behind the value
attribute of the UIData
component) is preserving exactly the same data model during the request of the form submit. If you want to keep your bean request scoped, then the easiest way is to reference the bean in an <a4j:keepAlive>
tag.
更新根据您的问题更新:进去的命令链接/按钮UIData
等组件作为<h:dataTable>
,<rich:dataTable>
等来的工作,你需要确保其持有的数据模型(无论是背后的豆value
中的属性UIData
成分)保持准确表单提交请求期间的相同数据模型。如果您想保持 bean 请求的范围,那么最简单的方法是在<a4j:keepAlive>
标记中引用 bean 。
回答by mahmut
<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) {
}
}