问题描述
我正在尝试使用飞碟将JSF页面转换为PDF.
I am trying to convert a JSF Page to PDF with Flying Saucer.
@ManagedBean
@SessionScoped
public class ReportController {
...
public void createPDF() {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
try {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new URL("http://myserver.com/report.xhtml").toString());
renderer.layout();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename="report.pdf");
OutputStream browserStream = response.getOutputStream();
renderer.createPDF(browserStream);
} catch (Exception ex) {
...
}
facesContext.responseComplete();
}
}
在/report.xhtml页面中,我有一些backing bean参数,这些值应该出现在pdf中.但是他们没有.如果我访问xhtml页面,则值正确显示.我认为这是因为renderer.setDocument(String uri)为从指定的URL加载文档创建了新的连接(和新的会话).如何在当前会话中获取xhtml页面内容(包含所有参数值)?
In the /report.xhtml page I have some backing bean parameters, which values should appear in the pdf. But they does not. If I access the xhtml page, then values showing correctly.I think this is because renderer.setDocument(String uri) creates new connection (and new session) for load document from specified url. How can I get xhtml page content within my current session (with all parameters values)?
推荐答案
获取 HttpSession
通过 ExternalContext#getSession()
并将其ID添加为jsessionid
URL路径片段.
Grab the HttpSession
by ExternalContext#getSession()
and add its ID as jsessionid
URL path fragment.
HttpSession session = (HttpSession) externalContext.getSession(true);
String url = "http://myserver.com/report.xhtml;jsessionid=" + session.getId();
// ...
请注意,查询字符串(如果有的话)应该在此之后而不是之前.
Please note that the query string, if any, should come there after and not there before.
这篇关于JSF 2.0在当前会话中获取xhtml页面的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!