11 Replies Latest reply on Jun 1, 2009 4:28 PM by nbelaevski

    RF + JasperReports

    elf

      Hi
      I am trying to run simple JasperReport.
      When I saved it into file everything works fine.
      But, I want browser propose user to download report.

      There is modalPanel, there is HtmlAjaxCommandButton on panel.
      There is action attached to the button.

      It is my code:

      ServletOutputStream out = ((HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()).getOutputStream()
      
      byte[] bytes = null;
      ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).setContentType("application/pdf");
      
      JasperReport rep = getJasperReportObject(md);
      if (rep == null)
       return "";
      
      bytes = JasperRunManager.runReportToPdf(rep, new HashMap(), new JREmptyDataSource());
      out.write(bytes, 0, bytes.length);
      
      ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).setContentLength(bytes.length);
      
      ((HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse()).addHeader("Content-Disposition", "attachment; filename=" + md.getClassName() + ".pdf");
      
      out.flush();
      out.close();
      
      FacesContext.getCurrentInstance().getExternalContext().redirect(RichHelper.getRequest().getContextPath() + PORTAL_PAGE);
      FacesContext.getCurrentInstance().responseComplete();


      Page refreshed, but no request to download file.
      I am new to RF and to JasperReport, please help me. What I am doing wrong.

        • 1. Re: RF + JasperReports
          nbelaevski

          Hello,

          AJAX requests are invisible to end-user, so that's the reason why no dialog appears. Also please take a look to recently created issue: https://jira.jboss.org/jira/browse/RF-7019

          • 2. Re: RF + JasperReports
            elf

            In this case is the refreshing of whole page the good solution ?
            May be it will be better to open new clear page and from there propose to download file ? In case of yes coluld you please drop me a piece of code how I can do it ? Click on ajax button, open new page and call action from new page ?
            Or may be there is another good solution for this ?

            • 3. Re: RF + JasperReports
              nbelaevski

              Use h:commandButton to initiate download request or use a4j:commandButton and redirect to the file (e.g. redirect to servlet that will output the file)

              • 4. Re: RF + JasperReports
                elf

                nbelaevski, thank you for help.
                I think the best solution for me will be to open new page by clicking on the ajax button and then propose user to download file. I want existing page stay as is.
                Please drop me the piece of code how I can open new page by clicking on ajax button and fire action from this new page.
                Thanks in advance.

                • 5. Re: RF + JasperReports
                  nbelaevski

                  How about using rich:modalPanel component instead of creating new page?

                  • 6. Re: RF + JasperReports
                    elf

                    My ajax button is already on modalPanel (I wrote in first post).
                    If it is possible to download file from modalPanel, so may be there is mistake in my code above ?
                    If it is not possible to download file using ajax button on modalPanel, I would prefer to open new page and download file from there.
                    Thank you.

                    • 7. Re: RF + JasperReports
                      nbelaevski

                      Use h:commandButton and "target" attribute to open new page.

                      • 8. Re: RF + JasperReports
                        elf

                        nbelaevski, please post the piece of code. I don't understand how I can do that.
                        Thanks in advance.

                        • 9. Re: RF + JasperReports
                          nbelaevski

                           

                          <h:commandButton target="_blank">...</h:commandButton>
                          will open new popup window as a result of button action execution.

                          • 10. Re: RF + JasperReports
                            elf

                            nbelaevski, I can not find attribute TARGET in TLD.
                            Could you please point me where I can find it ?

                            P.S. I have been trying to open new page using javax.faces.component.html.HtmlCommandButtom.

                            • 11. Re: RF + JasperReports
                              nbelaevski

                              You are right, and I was wrong - very sorry for that. There's no such supported attribute at h:commandButton, only h:commandLink supports "target".