在jsf页面中使用commandButton下载文件 [英] Using a commandButton in a jsf Page to download a file

查看:121
本文介绍了在jsf页面中使用commandButton下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jsf页面中的commandButton下载文件。使用:JSF& Richfaces。

Using a commandButton in a jsf Page to download a file. Using: JSF & Richfaces.

我有一个表(扩展ExtendedDataModel实现可修改,可序列化)和一些数据,每行都有一个按钮下载。

I have a table (extends ExtendedDataModel implements Modifiable, Serializable) with some data and in each row a button "download".

<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);
    }

}
...
}

当我实现ExtendedDataModel时,问题就开始了。
起初我使用了h:commandLink,但是控制器方法从未被调用过...我试过试过......现在调用了正确的方法,但是(zip)文件内容显示在页面中。我想在页面中有一个按钮/链接,用户可以单击该链接下载文件。页面本身不应该改变。有任何想法吗?

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?

我可以创建一个servlet,但我不明白为什么ExtendedDataModel改变了里面链接的行为。

I can create a servlet, but i do not understand why the ExtendedDataModel changed the behavior of the links inside.

Edit1

我用过

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

之前。它适用于普通richfaces表,但不是当我在我自己的表中使用它扩展ExtendedDataModel时。

before. It works with the "normal" richfaces table, but not when i used it inside my own table which extends ExtendedDataModel.

编辑2 - 解决方案/解决方法

无法使用h :commandButton,.. Link ...在自制表内部,下载文件。我现在使用表中的一个按钮来渲染新的PanelGroup,并在新的PanelGroupt中使用第二个按钮来下载文件。
我为此搜索了很多,看起来像一个富有的面孔bug。

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.

推荐答案

你无法下载文件通过ajax请求。将 a4j:commandButton 替换为 h:commandButton

You can't download a file by ajax request. Replace a4j:commandButton by h:commandButton.

更新:获取 UIData 组件中的命令链接/按钮,例如< h:dataTable> < rich:dataTable> 等工作,你需要确保持有数据模型的bean( UIData 组件的属性后面的任何内容)保持完全相同表单提交请求期间的数据模型。如果你想保持bean请求作用域,那么最简单的方法是在< a4j:keepAlive> 标记中引用bean。

Update as 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.

这篇关于在jsf页面中使用commandButton下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆