[jboss-user] [JBoss Seam] - Re: how to download a file via Seam& JSF,

squ1rr3l do-not-reply at jboss.com
Wed Jan 23 22:04:22 EST 2008


Ok, I have my code working now.  There were a couple of things that didn't work:

1)  Initially I tried using the 

  | JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
  | 
for passing the outputStream from the Response object to the JasperExportManager.  AFAIK, that's what produced the "Writer not possible" error.

2)  In the session EJB, using FacesContext.getCurrentInstance() does not provide a usable context - at least not one that can be used to obtain the Response object.  So maybe that's usable in a POJO component, but not an EJB component, and that's possibly by design, I don't know.  Scope doesn't really seem to matter.

Anyway, what works is this:

    - In the session EJB:

  | @In 
  | private FacesContext facesContext;
  | private byte[] report1;
  | 
  | ...
  | 
  |      String reportName = "/reports/myreport.jrxml";
  |      CreateReport cr = new CreateReport();
  |      report2 = cr.getReport(reportName);
  |      try {
  |           HttpServletResponse response = (HttpServletResponse)
  |                facesContext.getExternalContext().getResponse();
  |           response.setContentType("application/pdf");
  |           OutputStream os = response.getOutputStream();
  |           os.write(report2);
  |           os.flush();
  |           os.close();
  |           facesContext.responseComplete();
  |      } catch(Exception ex) {
  |           ex.printStackTrace();
  |      }
  | 

Just for completeness - the CreateReport.getReport method just does the jasper stuff to load and fill the report, then return the byte array:

  | public byte[] getReport(String reportName)
  | {
  |      try
  |      {
  |            ServletContext sCtxt = ServletLifecycle.getServletContext();
  |            File reportFile = new File(sCtxt.getRealPath(reportName));
  |            if( !reportFile.exists()) {
  |                 throw new JRRuntimeException("File " + reportName + " not found.");
  |            }
  |       
  |            JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
  |            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
  |            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
  |      
  |            if (jasperPrint != null) {
  |                 byte[] reportPDF = JasperExportManager.exportReportToPdf(jasperPrint);
  |                 return reportPDF;
  |            } else {
  |                 throw new JRRuntimeException("null jasperPrint");
  |                 return null;
  |            }
  |      } 
  |      catch (Exception e)
  |      {
  |            e.printStackTrace();
  |      }   
  |      return null;
  | }
  | 

Thanks for the help!


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4122875#4122875

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4122875



More information about the jboss-user mailing list