1

这是流程:


0 - 用户转到 http:\ form_url
1 - 出现表单
2 - 用户填写字段
3 - 用户点击“保存并生成 PDF”
4a - 应用程序打开一个新的浏览器选项卡并显示 pdf
4b - 应用程序在原始标签

到目前为止,我已经能够完成 4a 或 4b。一个或另一个,而不是同时两个。

你能帮帮我吗,谢谢

4

1 回答 1

1

您不能向 1 个 HTTP 请求发送 2 个 HTTP 响应。

您需要让客户端发送 2 个 HTTP 请求。在这种特殊情况下,最简单的方法是将临时生成 PDF 所需的数据存储在会话中,并使用 JavaScriptwindow.open()在新窗口中触发新请求,从而触发 PDF 生成。例如

id = UUID.randomUUID().toString();
Data data = collectDataWhichIsNecessaryForGeneratingPdf();
externalContext.getSessionMap().put(id, data);
return "brandNewEmptyform";

以全新的空形式

<h:outputScript rendered="#{not empty bean.id}">
    window.open('somePdfServlet?id=#{bean.id}');
</h:outputScript>

然后在映射到的servlet中/somePdfServlet

String id = request.getParameter("id");
Data data = (Data) session.getAttribute(id);
session.removeAttribute(id);
// Now generate PDF based on data and write to response the usual way.
于 2012-10-24T16:30:22.393 回答