1

我正在使用 springbatch 调用使用 JSF 的碧玉报告。问题是当调用该方法时,facescontext 始终为空,因此我可以获得会话。对于我读过的不同问题,我意识到问题是因为批处理不在 JSF 生命周期中。如何创建 facescontext 的实例,以便获取会话信息?我尝试制作一个FacesServlet,但我不确定在什么时候,我可以调用它,以及如何获取请求和响应信息。

  HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + nombreReporte + ".pdf");
        ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
        FacesContext.getCurrentInstance().responseComplete();
4

2 回答 2

0

我发布的代码是我用来从我的托管 bean 生成 JasperReports 的代码,它可以工作:

      public ServletContext getContext() {
        return (ServletContext)getFacesContext().getExternalContext().getContext();
    }

    public HttpServletResponse getResponse() {
        return (HttpServletResponse)getFacesContext().getExternalContext().getResponse();
    }

    public static FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    public void generate() {
        HttpServletResponse response =           (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        ServletOutputStream outStream;

        try {
            outStream = response.getOutputStream();
        } catch (IOException e) {
            _logger.severe("IOException getting outputStream during report generation!!", e);
            e.printStackTrace();
        }
        ServletContext context = getContext();
        InputStream fs = context.getResourceAsStream("/Reports/" + jrxml);

//Following there is a lot of code for generating my custom reports
   ........................

//At the end
   FacesContext.getCurrentInstance().responseComplete();
}
于 2014-04-02T19:38:14.753 回答
0

书中最古老的把戏。请参阅:从 servlet 访问 FacesContext

于 2014-04-02T17:39:32.820 回答