JSF 2.0 Get content of xhtml page within current session

吃可爱长大的小学妹 提交于 2019-12-01 08:57:07

问题


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 load document from specified url. How can I get xhtml page content within my current session (with all parameters values)?


回答1:


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.



来源:https://stackoverflow.com/questions/7247831/jsf-2-0-get-content-of-xhtml-page-within-current-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!