1

I'm using springbatch to call a jasper report using JSF. The problem is when a call the method, the facescontext is always null, so I can get the session. For different issues that I had read, I realize that the problem is because the batch process is not in the JSF lifecycle. HOw can I make an instance of facescontext, so I can get the session information? I try to make a FacesServlet, but I'm not sure in which moment, can I call it, and how to obtain the request and response information.

  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();
user3490001
  • 31
  • 1
  • 4
  • `FacesContext` is designed to be a singleton. That's the only way to work with it. The problem here is, do you have really a Servlet Context in your Spring Batch context? Let me hesitate about it. Unless you have it, how do you pretend to access the HttpSession information? – Aritz Apr 02 '14 at 16:02

2 Answers2

0

The oldest trick of the book. See: Access FacesContext From servlet

lu4242
  • 2,318
  • 1
  • 15
  • 15
  • 2
    OP is looking for the wrong solution to the problem. OP thus ultimately needs a handle to the HTTP session (for some unclear reason). In order to get that, OP just needs a handle to the HTTP request first. Manually creating a `FacesContext` instance isn't going to solve the problem in a better way as you still need a HTTP request *and* HTTP response in order to successfully create one. `FacesContext` is after all largely also just a wrapper over the servlet API. Why not just use servlet API directly then if you already have it at hands in a servlet based framework? – BalusC Apr 03 '14 at 05:47
  • If you have FacesContext, you have also ExternalContext, and with that class you have everything else. – lu4242 Apr 03 '14 at 10:15
0

The code i'm posting is the one i use to generate JasperReports from my managed bean, and it works:

      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();
}
Endrik
  • 2,238
  • 3
  • 19
  • 33