我是 Spring MVC 的新手,但它的功能给我留下了深刻的印象。
我正在使用 3.1.0-RELEASE,并且我必须显示 PDF 来响应 form:form 提交。
这是我在控制器中编写的(小)代码:
@RequestMapping(value = "new_product", method = RequestMethod.POST, params = "print")
@ResponseBody
public void saveAndShowPDF(ModelMap map, ShippingRequestInfo requestInfo, HttpServletRequest request, HttpServletResponse httpServletResponse) throws IOException {
saveProductChanges(map, requestInfo, request, httpServletResponse);
httpServletResponse.setContentType("application/pdf");
byte[] pdfImage = productService.getPDFImage(requestInfo.getRequestId());
httpServletResponse.getOutputStream().write(pdfImage);
}
此代码将 PDF byte[] 发送回原始窗口。
如何让 PDF 在单独的窗口中显示,以便我仍然可以使用原始浏览器窗口来显示其他内容?最好的方法是使用客户端 PDF 查看程序(Adobe Reader、FoxIt 等)显示 PDF,但我更愿意将 PDF 显示在单独的浏览器窗口中。
EDIT:我决定设置 Content-Disposition,以便浏览器显示一个保存/打开框,用户可以在其中打开 Adobe(但会丢失主浏览器页面)。
httpServletResponse.setHeader("Content-Disposition","attachment;filename=cool.pdf");
感谢大家!