Java 在jsf页面中使用commandButton下载文件

Java 在jsf页面中使用commandButton下载文件,java,jsf,richfaces,Java,Jsf,Richfaces,在jsf页面中使用commandButton下载文件。使用:JSF&Richfaces 我有一个表(ExtendedDataModel实现了可修改、可序列化),其中包含一些数据,每行都有一个按钮“下载” 当我自己实现ExtendedDataModel时,问题就开始了。 起初我使用h:commandLink,但从未调用控制器方法。。。我试了又试。。。现在调用了正确的方法,但是(zip)文件内容显示在页面中。我希望在页面中有一个按钮/链接,用户可以单击该按钮/链接下载文件。页面本身不应更改。有什么想

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

我有一个表(ExtendedDataModel实现了可修改、可序列化),其中包含一些数据,每行都有一个按钮“下载”

当我自己实现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中的第二个按钮下载文件。
为此,我在谷歌上搜索了很多,看起来像是一个RichFaces bug。

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



更新根据您的问题更新:要在
UIData
组件(如
等)中获取命令链接/按钮,您需要确保保存数据模型的bean(无论
UIData
组件的
属性后面是什么)在表单提交请求期间保留完全相同的数据模型。如果希望保持bean请求的作用域,那么最简单的方法是在
标记中引用bean

h:commandButton不工作,因为我扩展了ExtendedDataModel。我使用了“普通”的richfaces表-h:commandButton工作起来很有魅力。然后,我用自己的:h:commandButton替换了该表,不再调用该方法。。它刚刚刷新了网站。类似site.jsf的东西,您确实需要触发一个同步请求。JavaScript无法正确处理二进制响应,因为由于安全限制,它没有强制另存为对话或写入本地磁盘的功能。尝试在
a4j:commandButton
中设置
ajaxSingle=“false”
(但是我认为这不会正常工作)。我不做RichFaces,但通过谷歌搜索这个问题,我知道你不是唯一一个在这个问题上挣扎的人。还有人建议使用
a4j:htmlCommandLink
作为解决方案,试试看。
<h:commandButton id="getDownload" value="download"  action="#{controller.download()}"   >

</h:commandButton>
<h:commandLink id="getDownload" value="download" action="#{controller.download}">                                                       
                            <f:setPropertyActionListener target="#{controller.idString}" value="#{item.id}" />                              
                        </h:commandLink>
<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) {

    }

}