开发者

JSF 2.0 Get content of xhtml page within current session

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();
    }
}

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 开发者_如何学JAVAload document from specified url. How can I get xhtml page content within my current session (with all parameters values)?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜