如何在 Tapestry5 中同时返回页面和流?

发布于 2024-10-06 02:41:12 字数 371 浏览 7 评论 0原文

我正在使用 Tapestry 5 框架/javascript 开发一个 Web 应用程序项目。该应用程序需要很少的输入并返回 pdf 文件。

我收到了一个新要求,即当用户单击表单提交按钮时禁用该按钮。在生成报告之前,该按钮应保持禁用状态。我计划编写 javascript 来禁用提交按钮。但是如何在返回 pdf 文件时重新启用按钮?

我已经阅读了 Tapestry 文档。根据文档,有效的返回类型为“Nothing、String、Class、Page、Link、Stream”。如果我返回页面,我可以重新启用提交按钮。如果我返回一个流,用户将能够下载 pdf。但我想两者兼得。有什么方法可以返回多个对象(可能是另一个对象中的一个对象)。 (或者)有人可以建议解决这个问题吗?

谢谢

I am working on a web application project using Tapestry 5 framework/javascript. This application takes few inputs and returns pdf files.

I got a new requirement of disabling the form submit button when the user clicks it. The button should stay disabled till the reports are generated. I am planning to write javascript for disabling the submit button. But how can I reenable the buttons while returning pdf file?

I have gone through Tapestry documentation. As per the doc, the valid return types are "Nothing, String, Class, Page, Link, Stream". If I return a page, I can reenable the submit button. If I return a stream, the user will be able to download the pdf. But I want to do both. Is there any way by which I can return multiple objects (may be one object inside another object). (or) Can someone suggest any solution to this problem?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-10-13 02:41:12

我猜您的工作流程是表单提交处理程序定向到在 onActivate 方法中返回流(pdf)的页面。我还猜测您设置了响应标头,以便下载文件而不是由浏览器打开文件。

如果这样做,浏览器通常会创建一个“文件另存为”对话框,并且当前页面永远不会离开(不刷新)。您也永远不会收到任何回调告诉您文件已下载。

因此,通过上述工作流程,您将无法重新启用按钮作为对下载启动或完成的响应。这是因为流响应只不过是任意二进制数据,而不是浏览器能够执行的 html/javascript。您无法返回 html/javascript + 二进制文件数据的混合响应。

您可以实现的最好方法是禁用该按钮一段时间,然后重新启用它以防止双击。如果您不想给应用程序增加太多复杂性,我推荐这种方法。

但是,您可以更改工作流程,在表单提交和下载之间插入额外的请求-响应。

即,您可以重新加载当前页面,然后请求下载,而不是直接定向到返回流的页面:

  1. 您的表单提交将您的 pdf 生成参数保存到会话并返回响应(重新加载页面),
  2. 新页面会一起加载使用元刷新或脚本指令重定向到下载网址,
  3. 调用下载网址返回流响应以及正确的标头集,以便用户看到“文件另存为”对话框

,可能还有其他方法可以实现您的目标也需要所需的功能,但是模式是相同的,因为您需要在表单提交和文件下载之间添加额外的请求-响应。

附:这并不是真正的挂毯问题,无论框架如何,您都会遇到相同的问题(除非框架做了一些特殊的事情来处理这种情况)。这就是 http/浏览器技术的工作原理。

I'm guessing your workflow is that the form submit handler directs to a page which returns a stream (the pdf) in the onActivate method. I'm also guessing your set the response headers so that the file is downloaded rather than opened by the browser.

if you do this, the browser typically creates a 'save file as' dialog and the current page is never navigated away from (does not refresh). you will never receive any callback telling you that a file has been downloaded either.

therefore with the above workflow, you will not be able to re-enable your button as a response to the download initiating or completing. this is because the stream response is nothing but arbitrary binary data and not html/javascript capable of being executed by the browser. you cannot return a mixed response of html/javascript + binary file data.

the best you could achieve would be to disable the button for a period of time, and then re-enable it to prevent double clicking. if you don't want to add too much complexity to your application i recommend this approach.

however, you could change your workflow inserting an extra request-response between your form submission and your download.

i.e. instead of directing straight to the page which returns the stream, you could reload your current page which in turn requests the download:

  1. your form submission saves your pdf generation parameters to the session and returns response (reloading the page)
  2. the new page loads along with a meta-refresh or script directive to redirect to the download url
  3. the download url is called returns the stream response along with the correct headers set so that the user sees the 'save file as' dialog

there are probably other approaches which would achieve your desired functionality too however the pattern will be the same in that you will need an additional request-response between form submission and file download.

ps. this is not really a tapestry problem, you will come across the same issue regardless of framework (unless the framework does something special to handle this case). it is just how http/browser technology works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文