JasperReport report development (1)-principles, jasperreport

Source: Internet
Author: User

JasperReport report development (1)-principles, jasperreport
1. JasperReport Introduction

JasperReport is an open-source Java report engine. Unlike other reporting tools, for example, Crystal Reports are based on Java and do not have their own expression syntax. Jasper Reports provides rich content to the screen, to the printer, or to PDF, HTML, XLS, RTF, ODT, CSV, TXT, and XML files. Jasper Reports is a Java class library that needs to be embedded into Java applications. Jasper Report has the following features:

  • Flexible report layout;
  • Data can be displayed in text or graphics;
  • Developers can provide data in multiple ways;
  • Data from multiple data sources can be accepted;
  • Generate a watermark (the watermark is placed in the secondary image of the main image in this way );
  • Subreports can be generated;
  • Reports can be exported to multiple formats.
2. Development Process

JasperReport is fully written in Java and can be used to generate dynamic content in various Java applications, including J2EE and Web applications. It is mainly used to generate page-oriented and printed documents. The following flowchart describes a typical report development workflow.

 

As shown in, JasperReport is developed in four stages:

(1) design reports

This stage is to create a jrxml file, which is an XML file containing the report layout definition. You can use JasperSoft Studio or iReport to create a jrxml file.

(2) compile the report

In this phase, the source file (*. jrxml) is compiled into a binary file (*. jasper), which can run with the application.

(3) execute the report

This step is to fill the data in the application in the report template, that is, the jasper file. This phase generates a jasper print file (*. jrprint) that can be used to print or export reports.

(4) export the Report to the required format

JasperReport provides various export formats, such as HTML, PDF, and Excel. You can export the required report format at this stage.

3. Implementation Principle

JasperReport Library provides a wide range of class libraries for report development. shows the process of generating, printing, and exporting reports.

 

The development process is as follows:

(1) Develop a report design file, which is also a *. jrxml file.

(2) Compile the *. jrxml file using the JasperCompileManager tool provided by JasperReports, and generate a *. jasper file after compilation.

(3) Use the JasperFillManager tool provided by JasperReports to fill in the compiled *. jasper file and generate a *. jrprint file.

(4) use the export manager JasperExportManager or JRXxxExporter to export *. jrprint files to report files in various formats. You can also use the JRViewer tool class to directly browse reports. You can also use the print manager JasperPrintManager to print reports.

4. Related Implementation classes

The JasperReport development process mainly involves the following classes:

(1)Net. sf. jasperreports. engine. design. JasperDesign

This class can be obtained after the built-in XML Parser of the JasperReport class library parses the XML report design [2]. It corresponds to the form of the report template file in the memory, that is, the *. jrxml file.

(2)Net. sf. jasperreports. engine. JasperReport

The instance of this class contains a compiled report design object. It is generated after the report is compiled, but data has not been filled in. During compilation, JasperReport must generate a temporary class file to save report expressions, such as variable expressions, text, image expressions, and group expressions. This temporary Java Source File is dynamically compiled, and the compiler uses the compiler class (compiler class) used to execute the application in JDK ). If tools.jar classpathis in, the compilation process will compile javac.exe in the background. The compiled bytecode is stored in the JasperReport class to fill data during execution and assign values to expressions. The format of the binary report file in the memory, that is, the corresponding *. jasper file.

(3)Net. sf. jasper. engine. JasperPrint

After a report is filled with data, this document appears as an instance of the JasperPrint class. This class can be viewed directly using the built-in viewer of JasperReport, serialized to the hard disk for later use, or sent to the Internet. An instance of this class is the product of the report filling process. It can be exported to various popular formats such as PDF, HTML, and XML by using the Export method in the JasperReport class library.

(4)Net. sf. jasperreports. engine. xml. JRLoader

The loader is used to report various major stages of table generation, such as compilation and filling. You and the engine can use this class to load the required serialized objects such as file, URLs, and intput stream. The most interesting function of this class is loadOnjectFromLocation (String location) [3]. When you use this class to load objects from a specified location, this function will first interpret the location as a legal URL. If the parsing fails, the function will assume that the provided location is a file name on the hard disk and will try to read it. If the file is not found at the specified location, it locates a resource corresponding to the location through classpath. If all efforts fail, an exception is thrown.

(5)Net. sf. jasperreports. engine. JasperCompileManager

This is a compilation-related class that allows us to design a report document (*. jrxml file) is compiled into a binary file (*. jasper file ). In addition, it can also directly. sf. jasperreports. engine. design. jasperDesign (*. jrxml file in the memory format) object compiled into net. sf. jasperreports. engine. jasperReport object.

(6)Class net. sf. jasper. engine. JasperFillManager 

This class is used to populate the report data. Report filling provides database connection for report data query, and sets parameters for the report. Before filling, It is the *. jasper file. After filling, it becomes the *. jrprint file. This is a file that can be displayed or exported as a report. This class provides many methods to accept various types of report design, which can be an object, file, or input stream. Its output results are also diverse: file, Object, and output Stream.

(7)Net. sf. jasperreports. engine. JasperPrintManager

Used to print a *. jrprint file. In JapserReports, we can use this class to print reports. It includes all the printing functions. It provides the method to print the entire document or part of the document and display the Print dialog box. This class can be used to display a page of the JasperReports document as a java. awt. Image object.

(8)Net. sf. jasperreports. engine. JasperExportManager

This management class provides different methods for data from different sources and locations (files, input and output streams, etc. Exports displayed reports to report files in various formats, such as PDF, HTML, XML, and other formats.

(9)Net. sf. jasperreports. engine. export. JRXxxExporter

These are a series of File Export tools used to export *. jrprint files to report files in the corresponding format. For example, XSL, PDF, HTML, XML, CSV, RTF, TXT, and other formats. JRXlsExporter, jrw.exporter, JRXmlExporter, JRCsvExporter, JRHtmlExporter, JRTextExporter, and JRRtfExporter.

(10)Net. sf. jasperreports. engine. JasperRunManager

This class can directly set *. the jasper file is exported as a report file of various formats. Sometimes we do not want to generate the intermediate net. sf. jasperreports. engine. the JasperPrint object directly generates the required document format, such as PDF or HTML.

5. Report Data Source

The Jasper Report Engine obtains data from data sources, including databases, XML files, object arrays, and objects in a set. We have introduced how to use JasperFillManager to populate the report data. Specifically, the fillReportXXX () method obtains the data source for filling.

(1) JDBC Data Source

The JRResultSetDataSource class is associated with a java. SQL. ResultSet object. This is the most common data source for extracting report data from relational databases. If java. SQL. Connection is passed to the engine, it first executes the relevant query and returns the objects in java. SQL. ResultSet IN A JRResultSetDataSource instance.

(2) JavaBean Data Source

The JRBeanArrayDataSource class and JRBeanCollectionDataSource class enable arrays or sets of JavaBean objects that can be packaged separately. Each object in an array or set is considered as a record in this type of data source. The ing between a specific JavaBean attribute and corresponding report fields is performed through naming conventions. The report field name must be the same name of the standardized JavaBean attribute of the specified JavaBeans.

(3) Map-based data sources

The java. util. Map object in the memory where the parent application has been stored provides the implementation class JRMapArrayDataSource and JRMapCollectionDataSource for data filling. Each ing object in the encapsulated array or set is considered as a virtual record in the data source. The value of each report field is extracted from the ing using the report field name as the key.

(4) TableModel Data Source

Data of many client applications is displayed in tables. In many applications, a common requirement is to allow users to print reports in the form of this table. Implement a job that uses the JRTableModelDataSource class to generate Swing application reports in table format. This class encapsulates a javax. swing. table. TableModel object. The packaged TableModel objects can be accessed by their names or their 0-based indexes.

(5) XML Data Source

The JRXmlDataSource class is based on DOM. It uses XPath expressions to select the implementation of the data source of XML documents. Records in the XML data source are represented by node elements selected by the XPath expression. Field Values are retrieved by each record using the XPath expression provided by the field description (JRXML <fieldDescription> element.

(6) CSV Data Source

JRCsvDataSource represents the implementation of data sources for retrieving data from structured text files, usually CSV. Field Values are being retrieved using their column indexes.

(7) XLS Data Source

JRXlsDataSource represents the data source implementation of the Excel files it retrieves. The ing of report fields to this data source is also based on field column indexes.

(8) Empty data source

Class JREmptyDataSource, simulating a specified number of data sources with internal virtual null records. It is a tool provided by the user interface to provide basic report preview functions, or in a special report template, or for testing and debugging purposes.

So far, the whole process and implementation principles of JasperReport report development have been introduced. After learning about this, I will be clear about it later.

References

[1] JasperReport online tutorial. http://gitbook.net/jasper_reports/jasper_getting_started.html

[2] JasperReports report development process-http://topmanopensource.iteye.com/blog/1866879

[3] JasperReport API. http://jasperreports.sourceforge.net/api/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.