11 Replies Latest reply: Oct 13, 2012 7:42 PM by dvohra21 RSS

    jasper reports and jsf web page

    steve kalenga
      hi guys
      i m using jdev 11 release 2
      i m developing an application using jsf pages
      i using ireport 4.7.0
      i create a .jasper file
      copy it on the directory of my application
      i created a jsf page ,but i dont know where to paste the code to generate the report
      i did use the tutorial on
      http://www.theregister.co.uk/2006/10/24/jasperreports_tutorial/
      when i click on the jsp page source is an html
      but on the jsf page source is an xml.

      any help

      regards

      steve kalenga
        • 1. Re: jasper reports and jsf web page
          in the line of fire
          Can you explain what exactly you are looking for ?
          • 2. Re: jasper reports and jsf web page
            steve kalenga
            thanks,
            i just want anyone to assist how to create a report using jasper report (ireport) either PDF ,excel,HTML
            but my web page is a .jsf not a jsp

            regards


            Steve kalenga
            • 3. Re: jasper reports and jsf web page
              in the line of fire
              There will be no issue i guess. Create a jrxml file and call it from your bean class. Google a bit as this topic is already covered.
              • 4. Re: jasper reports and jsf web page
                steve kalenga
                thanks again i did the googling since
                what i want to know is how to create that bean.

                i m a bit confuse of the creation of the bean you refer me too
                just a simple steps

                regards
                • 5. Re: jasper reports and jsf web page
                  in the line of fire
                  I m just pasting an extract of the code. Call this from BB by passing the require values
                      public void downloadStatement(FacesContext facesContext, java.io.OutputStream outputStream) {
                          
                          Long statementRowId = (Long)EL.get("#{requestScope.statementRowId}");
                          //Approach 1 - getResourceAsStream() - will look in all classpath entires for the specified file. For this the report was aaded as a jar
                          //InputStream ios = Thread.currentThread().getContextClassLoader().getResourceAsStream("StatementReport.jrxml");
                          //end approach 1
                          //Approach 2 - this will only look in the spcific folder
                          try {
                              ExternalContext externalContext = javax.faces.context.FacesContext.getCurrentInstance().getExternalContext();
                              ServletContext servletContext = (ServletContext) externalContext.getContext();
                              String webContentRoot = servletContext.getRealPath("/");
                              _logger.fine("WebContentRoot is : " + webContentRoot);
                              String path = new File(".").getAbsolutePath();
                              _logger.fine("Absolute Path :" + path);
                              File reportsDir = new File(webContentRoot + "\\com\\xxx\\ui\\reports\\JasperReport.jrxml");
                              InputStream ios = new FileInputStream(reportsDir);
                              //end Approach 2
                              Map parameters = new HashMap();
                              parameters.put("PAR_CUSTOMER_ID", customerId.toString());
                              parameters.put("SUBREPORT_DIR", "./");
                              parameters.put("rowid", statementRowId.toString());
                              Connection connection = null;       
                              HttpServletResponse response =  (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
                              ServletOutputStream servletOutputStream;
                              servletOutputStream = response.getOutputStream();
                              byte[] bytes = null;
                              //this will give us the existing connection from the AM
                              ob = bc.getOperationBinding("getCurrentConnection");
                              ob.execute();
                              connection = (Connection)ob.getResult();
                              //This will create a new connection alltogether.
                              /* Context ctx = new InitialContext();
                              Context envCtx = (Context)ctx.lookup("");
                              DataSource ds = (DataSource)envCtx.lookup("jdbc/lm");
                              if (ds != null) {
                                      connection = ds.getConnection();
                              }  */
                              JasperDesign jasperDesign = JRXmlLoader.load(ios);
                              JasperReport jasperReport =   JasperCompileManager.compileReport(jasperDesign);
                              bytes = JasperRunManager.runReportToPdf(jasperReport, parameters,  connection);
                              response.addHeader("Content-disposition",  "attachment;filename=OnlineStatement.pdf");
                              response.setContentType("application/pdf");
                              response.setContentLength(bytes.length);
                              servletOutputStream.write(bytes, 0, bytes.length);
                              servletOutputStream.flush();
                              servletOutputStream.close();
                              facesContext.responseComplete();
                          }catch (IOException e) {
                              _logger.severe("IOException", e);
                              e.printStackTrace();
                          }catch (JRException e) {
                              _logger.severe("JRException", e);
                              e.printStackTrace();
                          }catch(Exception e){
                              _logger.severe("Exception", e);
                              e.printStackTrace();
                          }
                      }
                  • 6. Re: jasper reports and jsf web page
                    steve kalenga
                    thanks for the code and i manage to create the bean
                    regards
                    • 7. Re: jasper reports and jsf web page
                      steve kalenga
                      hi i create my bean
                      then running my application it create only the pdf,html but no data
                      could please check where i did mistake

                      package deployPro.view;

                      import javax.faces.bean.ManagedBean;
                      import javax.faces.bean.RequestScoped;
                      import java.io.*;
                      import java.util.*;
                      import java.sql.Connection;
                      import java.sql.SQLException;
                      import javax.faces.context.FacesContext;
                      import javax.faces.event.ActionEvent;
                      import javax.sql.DataSource;
                      import javax.naming.InitialContext;
                      import javax.naming.NamingException;
                      import net.sf.jasperreports.engine.*;
                      import net.sf.jasperreports.engine.design.JasperDesign;
                      import net.sf.jasperreports.engine.export.JRXlsExporter;
                      import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
                      import net.sf.jasperreports.engine.xml.JRXmlLoader;
                      import net.sf.jasperreports.view.JasperViewer;
                      import java.sql.DriverManager;

                      @ManagedBean(name="CollegeReportsBean")
                      @RequestScoped
                      public class reportsClass {
                      JasperReport jasperReport;
                      JasperPrint jasperPrint;

                      public reportsClass() {
                      }


                      public static Connection establishConnection() throws ClassNotFoundException {
                      Connection connection = null;
                      try
                      {
                      Class.forName("oracle.jdbc.driver.OracleDriver");
                      String oracleURL = "jdbc:oracle:thin:@localhost:1521:xe";
                      connection = DriverManager.getConnection(oracleURL,"username","password");
                      connection.setAutoCommit(false);
                      }
                      catch(SQLException exception)
                      {
                      exception.printStackTrace();
                      }
                      return connection;

                      }

                      public void welcome(ActionEvent actionEvent) throws FileNotFoundException, JRException, NamingException, SQLException, IOException, ClassNotFoundException {

                      Connection connection = establishConnection();
                      HashMap jasperParameter;
                      jasperParameter = new HashMap();


                      jasperReport = JasperCompileManager.compileReport("C:\\JDeveloper\\mywork\\CollegeInformationSystem\\CollegeViewController\\report2.jrxml");

                      jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, connection);
                      JasperExportManager.exportReportToPdfFile(jasperPrint, "C://sample_report1.pdf");
                      JasperExportManager.exportReportToHtmlFile(jasperPrint, "C://sample_report.html" );

                      JRXlsExporter exporter;
                      exporter = new JRXlsExporter();
                      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                      exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C://simple_report.xls" );
                      }
                      }
                      • 8. Re: jasper reports and jsf web page
                        in the line of fire
                        You need to flush and close the streams
                                    response.setContentType("application/pdf");
                                    response.setContentLength(bytes.length);
                                    servletOutputStream.write(bytes, 0, bytes.length);
                                    servletOutputStream.flush();
                                    servletOutputStream.close();
                                    facesContext.responseComplete();
                        • 9. Re: jasper reports and jsf web page
                          steve kalenga
                          i still have the same problem the jasperparameters still underline with error message "unchecked conversion from hashmap to <string,object> unsound but tolorated"

                          regards
                          • 10. Re: jasper reports and jsf web page
                            dvohra21
                            Are JSF and managed bean required? JSP may be used in JDeveloper.
                            http://www.theregister.co.uk/2006/10/24/jasperreports_tutorial/
                            • 11. Re: jasper reports and jsf web page
                              dvohra21
                              Use the latest jasperreports jar and Apache POI.

                                   JAR/ZIP     Download URL
                              jasperreports-4.7.0.jar     http://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%204.7.0/
                              itext-2.1.0     http://mirrors.ibiblio.org/pub/mirrors/maven2/com/lowagie/itext/2.1.0/itext-2.1.0.jar
                              commons-beanutils-1.8.3-bin.zip     http://commons.apache.org/beanutils/download_beanutils.cgi
                              commons-digester-2.1.jar     http://commons.apache.org/digester/download_digester.cgi
                              commons-logging-1.1.1-bin     http://commons.apache.org/logging/download_logging.cgi
                              poi-bin-3.8-20120326 zip or tar.gz      http://poi.apache.org/download.html#POI-3.8

                              Edited by: dvohra16 on Oct 13, 2012 5:41 PM