You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "Delbrouck, Henri-Philippe" <he...@siemens.com> on 2005/08/09 10:01:05 UTC

MyFaces and JasperReport

Hi,

Could anybody tell me if it is possible to use JasperReport with MyFaces in
order to generate pdf and xls file.
Is there any documentation about that.

Thanks for any help.

Henri-Philippe Delbrouck




Re: Double Click for file download was ->RE: MyFaces and JasperReport

Posted by Carsten Burghardt <ca...@cburghardt.com>.
Am Tuesday 09 August 2005 15:43 schrieb albartell:
> The only way I have been able to get around it in some instances is to use
> a commandLink tag and do a target blank to a jsp page that reads in url
> parms and determines what it needs to download.  This works because I never
> run the ctx.responseComplete() method.

Can you provide details about this? I have the same problem - the action only 
works the second time.


Carsten

Double Click for file download was ->RE: MyFaces and JasperReport

Posted by albartell <al...@gmail.com>.
Jozef, have you gotten around the problem of a double click to get back to
the original form state.  Here is my problem. Whenever I use
ctx.responseComplete() it always seems to mess with the state of the
application. By that I mean when I click the button the first time it
submits the form and executes and displays the new page just fine.  But when
I go back to the parent page and click any button on the page it does a full
trip to the server without executing the action method. Then the second time
I hit the button it works just fine, but then ctx.responseComplete() is
executed again and the cycle starts all over again.

The only way I have been able to get around it in some instances is to use a
commandLink tag and do a target blank to a jsp page that reads in url parms
and determines what it needs to download.  This works because I never run
the ctx.responseComplete() method.

Does anyone know if responseComplete() has a bug in it, or if we are using
it incorrectly?

Aaron Bartell

-----Original Message-----
From: Jozef Hribik [mailto:jozef.hribik@apsoft.sk] 
Sent: Tuesday, August 09, 2005 6:39 AM
To: MyFaces Discussion
Subject: Re: MyFaces and JasperReport

Hi Henri,
 
1. put commandButton on JSF to call action method of managed bean; the PDF
will open in new browser window.
<h:form id="yourform" target="_blank">
<h:commandButton styleClass="btn" value="#{msg.btn_pdf}" 
action="#{reportMBean.createPdf}" />

2. and use something like this in your reportMBean

  public String createPdf() {

    FacesContext ctx = FacesContext.getCurrentInstance();
   
    if(!ctx.getResponseComplete()) {
      try {
        String baseDir = getBaseDir(ctx);
        HttpServletResponse response = (HttpServletResponse)
ctx.getExternalContext().getResponse();
       
        byte[] bytes = buildContent(baseDir);
       
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;
filename=\"your_report_name.pdf\"");
        response.setHeader("Cache-Control", "max-age=10");
        response.setContentLength(bytes.length);

        OutputStream output = response.getOutputStream();
        output.write(bytes, 0, bytes.length);
        output.flush();
        output.close();
       
        ctx.responseComplete();
      }
      catch(BusinessException ex) {
// our BusinessException handling
        addBusinessExceptionMsg(ex);
        return NAV_SAME_PAGE;
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    return null;
  }
 
  // we have all report-files in "/WEB-INF/report/"
  private String getBaseDir(FacesContext ctx) {
    String path = ((ServletContext)
ctx.getExternalContext().getContext()).getRealPath("/WEB-INF/");
    return path + File.separator + "report" + File.separator;
  }
 
  private byte[] buildContent(String baseDir) throws Exception {
   
    Map parameters = new HashMap();
    parameters.put("ReportTitle", "your_title");
    // etc. put all other needed parameters
    parameters.put("REPORT_RESOURCE_BUNDLE", getResourceBundle());
    parameters.put("REPORT_LOCALE", getResourceLocale());
   
    List dataList = yourBusinessService.getDataForReport(your_parameters);
   
    return JasperRunManager.runReportToPdf(baseDir +
"your_report_name.jasper",
                                                 parameters,
                                                 new
JRBeanCollectionDataSource(dataList));
  }
 
Good-luck
Jozef Hribik  

Delbrouck, Henri-Philippe wrote:

> Hi,
>
> Could anybody tell me if it is possible to use JasperReport with 
> MyFaces in order to generate pdf and xls file.
> Is there any documentation about that.
>
> Thanks for any help.
>
> Henri-Philippe Delbrouck
>
>
>
>
>
> __________ Informacia od NOD32 1.1189 (20050808) __________
>
> Tato sprava bola preverena antivirusovym systemom NOD32.
> http://www.eset.sk
>
>
> __________ Informacia od NOD32 1.1189 (20050808) __________
>
> Tato sprava bola preverena antivirusovym systemom NOD32.
> http://www.eset.sk



Re: MyFaces and JasperReport

Posted by Jozef Hribik <jo...@apsoft.sk>.
Hi Henri,
 
1. put commandButton on JSF to call action method of managed bean; the 
PDF will open in new browser window.
<h:form id="yourform" target="_blank">
<h:commandButton styleClass="btn" value="#{msg.btn_pdf}" 
action="#{reportMBean.createPdf}" />

2. and use something like this in your reportMBean

  public String createPdf() {

    FacesContext ctx = FacesContext.getCurrentInstance();
   
    if(!ctx.getResponseComplete()) {
      try {
        String baseDir = getBaseDir(ctx);
        HttpServletResponse response = (HttpServletResponse) 
ctx.getExternalContext().getResponse();
       
        byte[] bytes = buildContent(baseDir);
       
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline; 
filename=\"your_report_name.pdf\"");
        response.setHeader("Cache-Control", "max-age=10");
        response.setContentLength(bytes.length);

        OutputStream output = response.getOutputStream();
        output.write(bytes, 0, bytes.length);
        output.flush();
        output.close();
       
        ctx.responseComplete();
      }
      catch(BusinessException ex) {
// our BusinessException handling
        addBusinessExceptionMsg(ex);
        return NAV_SAME_PAGE;
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    return null;
  }
 
  // we have all report-files in "/WEB-INF/report/"
  private String getBaseDir(FacesContext ctx) {
    String path = ((ServletContext) 
ctx.getExternalContext().getContext()).getRealPath("/WEB-INF/");
    return path + File.separator + "report" + File.separator;
  }
 
  private byte[] buildContent(String baseDir) throws Exception {
   
    Map parameters = new HashMap();
    parameters.put("ReportTitle", "your_title");
    // etc. put all other needed parameters
    parameters.put("REPORT_RESOURCE_BUNDLE", getResourceBundle());
    parameters.put("REPORT_LOCALE", getResourceLocale());
   
    List dataList = yourBusinessService.getDataForReport(your_parameters);
   
    return JasperRunManager.runReportToPdf(baseDir + 
"your_report_name.jasper",
                                                 parameters,
                                                 new 
JRBeanCollectionDataSource(dataList));
  }
 
Good-luck
Jozef Hribik  

Delbrouck, Henri-Philippe wrote:

> Hi,
>
> Could anybody tell me if it is possible to use JasperReport with 
> MyFaces in order to generate pdf and xls file.
> Is there any documentation about that.
>
> Thanks for any help.
>
> Henri-Philippe Delbrouck
>
>
>
>
>
> __________ Informacia od NOD32 1.1189 (20050808) __________
>
> Tato sprava bola preverena antivirusovym systemom NOD32.
> http://www.eset.sk
>
>
> __________ Informacia od NOD32 1.1189 (20050808) __________
>
> Tato sprava bola preverena antivirusovym systemom NOD32.
> http://www.eset.sk