[jboss-user] [JBoss Seam] - Re: Generation of jasper reports in seam

jazir1979 do-not-reply at jboss.com
Tue May 22 21:14:51 EDT 2007


Hi,

I wrote my own JSF component that can view a report inside a div on a given page, and also export a report in a number of output formats.  I can't really share the component at this stage, but these snippets should help you get something up and running.  Hopefully in the future I can make my component more generic and get permission to contribute it in full.

To view the report, firstly I create the JasperPrint object which is the result of filling the report (JasperFillManager.fillReport()).  The following code is inside my customer JSF component and the context variable you see is the FacesContext...  however the export gets triggered directly from a Seam action, where you can inject the FacesContext using @In, so you should be able to do this without having to write a JSF component.


  |         if (jasperPrint != null) {
  |             final JRExporter exporter = new JRHtmlExporter();
  |             exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  |             exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, context.getResponseWriter());
  |             exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
  |             exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
  | 
  |             // images
  |             final HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
  |             request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
  |                     jasperPrint);
  |             exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, new HashMap<Object, Object>());
  |             exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
  |                     request.getContextPath() + "/jasperimg?image=");
  | 
  |             // export
  |             try {
  |                 context.getResponseWriter().write("<div class=\"reportview\">");
  |                 exporter.exportReport();
  |                 context.getResponseWriter().write("</div>");
  |             } catch (final JRException e) {
  |                 LOG.error("Report exporting error", e);
  |                 .....
  |             }
  |         }
  | 

To export the report in a whole new response

  |             final JRExporter exporter = JRExporterFactory.getExporter(outputType, jasperPrint, context);
  |             final ByteArrayOutputStream stream = new ByteArrayOutputStream();
  |             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, stream);
  | 
  |             // export
  |             try {
  |                 exporter.exportReport();
  |             } catch (final JRException e) {
  |                 LOG.error("Report exporting error", e);
  |                 ....
  |             }
  |             final HttpServletResponse response =
  |                 (HttpServletResponse) context.getExternalContext().getResponse();
  |             try {
  |                 final OutputStream responseStream = response.getOutputStream();
  |                 response.setContentLength(stream.size());
  |                 response.setContentType(contentTypes.get(outputType));
  |                 if (outputType != ReportOutputType.HTML) {
  |                     response.setHeader("Content-Disposition", "attachment; filename=report." + outputType);
  |                 }
  |                 responseStream.write(stream.toByteArray());
  |                 responseStream.flush();
  |                 responseStream.close();
  |                 response.flushBuffer();
  |             } catch (final IOException e) {
  |                 LOG.error(e);
  |                 ....
  |             }
  |             context.responseComplete();
  | 
  | 

I hope this helps you to get it working,
Daniel.



"d.solasa" wrote : Hi
  | 
  | I struck at the point to generate the jasper reports in seam ,ofcourse in jboss too.i even used the component that is given in the jboss site (but that was depricated).And i tried in all the ways that am supposed to generate the jasper reports in java.May be i think i need a jar file or any other configuration to generate jaspe reports in seam..
  | 
  | plz be help me..So urgent that i need to implement it in my project

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

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



More information about the jboss-user mailing list