H:的commandButton多个动作:下载文件和渲染阿贾克斯表 [英] h:commandButton multiple actions: download file and render ajax table

查看:155
本文介绍了H:的commandButton多个动作:下载文件和渲染阿贾克斯表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有2个命令按钮和一个列表框。基于列表框的选择,所产生的结果可以显示在一个下载已能够文件或呈现为一个HTML表格。该的getFile() code基于的 BalusC的PDF处理教程,而 getTable() resultTable

I currently have 2 command buttons and one listbox. Based on listbox selection, the result generated can be shown in a download-able file or rendered as an HTML table. The getFile() code is based on BalusC's PDF Handling tutorial, while getTable() sets resultTable.

<h:form>
<fieldset>
    <h:selectManyListbox id="listbox" value="#{form.items}">
        <f:selectItems value="#{form.allItems}">
    </h:selectManyListbox>
</fieldset>
<h:commandButton value="Get File" action="#{form.getFile}">
<h:commandButton value="Get Table" action="#{form.getTable}">
    <f:ajax render="result_table" execute="listbox" />
</h:commandButton>
<h:panelGrid id="result_table">
    <table>
        <thead></thead>
        <tbody>
            <ui:repeat var="table" value="#{form.resultTable}">
            </ui:repeat>
        </tbody>
    </table>
</h:panelGrid>

两个按钮都工作正常为止。不过,我想这两个操作结合成一个按钮。当我测试了这一点,有一个按钮,触发了两个动作,没有任何反应(无文件另存为对话框或表渲染)。这是因为一个动作是阿贾克斯还是因为其他的动作完成与 facesContext.responseComplete();

<h:commandButton value="Get Both" action="#{form.getBoth}">
    <f:ajax render="result_table" execute="listbox" />
</h:commandButton>

getBoth() {
    getTable();
    getFile();
}

此外,我想在那里,如果它被选中的复选框,另存为对话框弹出,表呈现。如果没有选择,只有表的呈现方式。

Additionally I would like a checkbox where if it is checked, save as dialog pops up and table is rendered. If it is not checked, only table is rendered.

推荐答案

不幸的是,这是不可能的HTTP。你可以只发送一个响应返回每个请求。不能合并包含PDF文件和Ajax响应到一个响应的响应。由于这是一个HTTP限制,JSF不能为你做任何多。另外,在下载使用Ajax的文件是不可能的,因为在所有的JavaScript无法强制浏览器弹出一个的另存为的对话,也没有由于安全限制任何访问本地磁盘文件系统。

Unfortunately, that's not possible with HTTP. You can send only one response back per request. You cannot merge the response containing the PDF file and the ajax response into one response. Since this is a HTTP restriction, JSF can't do any much for you. Also, downloading a file using Ajax is not possible at all since JavaScript can't force the browser to pop a Save As dialogue nor have any access to local disk file system due to security restrictions.

一个解决方法是火的两个的HTTP上的一个按钮请求,点击其中第二个请求返回内容处置:附件这样的其他要求响应保持不变。您可以通过添加一个的onclick 的命令按钮实现这一目标。

A workaround would be to fire two HTTP requests on a single button click where the second request returns Content-Disposition: attachment so that the response of the other request keeps untouched. You can achieve this by adding an onclick to the command button.

<h:commandButton onclick="window.location='/context/pdfservlet/filename.pdf'">

和创建PDF的servlet这大致模样这个FileServlet例子。正如你看到的,这是不可能来调用这个JSF动作。你必须重构PDF下载方法为的HttpServlet 类,它确实在的doGet作业()方法。对于JSF管理bean和servlet的之间的任何必要的沟通,你可以通过请求路径和参数使用会话范围或传递所需信息(只是PDF文件标识符?)。

and create a PDF servlet which roughly look like this FileServlet example. As you see, it's not possible to invoke a JSF action by this. You have to refactor the PDF download method to a HttpServlet class which does the job in doGet() method. For any necessary communication between the JSF managed bean and the servlet, you could use the session scope or pass the desired information (just the PDF file identifier?) by request path or parameter.

这篇关于H:的commandButton多个动作:下载文件和渲染阿贾克斯表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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