Jasperreport Report Development (i)--Principle Introduction

Source: Internet
Author: User
Tags xml parser xpath

1. Jasperreport Introduction

Jasperreport is an open source Java reporting engine, unlike other reporting tools such as Crystal Reports that are Java-based and do not have their own expression syntax. Jasper reports has the ability to provide rich content to the screen, to the printer, or to convert Pdf,html,xls,rtf,odt,csv,txt and XML files. Jasper reports is a Java class library that needs to be embedded in a Java application. The features of the Jasper report are as follows:

    • Have a flexible report layout;
    • You can display data in text or graphics;
    • Developers can provide data in a variety of ways;
    • Can accept data from multiple data sources;
    • Watermarks can be generated (watermarks are such that are placed in the secondary image of the main image);
    • can generate sub-reports;
    • Ability to export reports to multiple formats.
2. Development process

Jasperreport is written entirely in Java and can be used to generate dynamic content in a variety of Java applications, including J2ee,web applications. Its primary purpose is to assist in generating page-oriented, ready-to-print documents. The following flowchart describes a typical workflow for report development.

As shown, Jasperreport Report development is divided into four stages:

(1) Design report

This phase is the creation of the Jrxml file, which is an XML document that contains the report layout definition and can be created using either Jaspersoft Studio or ireport visual open source development tools.

(2) Compiling reports

This phase compiles the source file (*.jrxml) into a binary (*.jasper) file that can be run with the application.

(3) Execute the report

This step is to populate the data in the application with the report template, which is the Jasper file. This phase will produce a Jasper print file (*.jrprint) that can be used to print or export the report.

(4) Export the report to the desired format

Jasperreport provides various forms of export formats, such as: Html,pdf,excel and so on. This stage allows you to export the desired report format.

3. Principle of implementation

The Jasperreport library provides a rich class library for report development, which generates reports and prints and exports the entire process as shown.

The development process is as follows:

(1) Develop the report design file, that is, a *.jrxml file.

(2) Compile the *.jrxml file using the Jaspercompilemanager tool provided by JasperReports and compile it into a *.jasper file.

(3) Populate the compiled *.jasper file with the Jasperfillmanager tool provided by JasperReports and populate it with a *.jrprint file.

(4) Export the *.jrprint file to a report file in various formats using the Export Manager Jasperexportmanager or the file exporter in various formats jrxxxexporter. You can also use the Jrviewer tool class to browse reports directly. You can also use Print Manager Jasperprintmanager to print the report.

4. Related implementation Classes

In the Jasperreport development process, the following categories are mainly involved:

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

This class can be obtained after parsing the XML report design with the XML parser built into the Jasperreport class library [2]. It corresponds to the form of the report template file in memory, that is, the *.jrxml file.

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

An instance of this class contains a compiled report design object. The time to build it is after the report has been compiled, but it has not been populated with data. During compilation, Jasperreport needs to generate a temporary class file that holds the report expression, such as variable expressions, text, image expressions, group expressions, and so on. This temporary Java Source file is dynamically compiled, and the compiler uses the compiler class (compiler Class) that is used to execute the application in the JDK. If Tools.jar is not in Classpath, the compilation process will use Javac.exe for background compilation. The resulting bytecode is stored in the Jasperreport class, which is used to populate the data and assign values to the expression during the execution period. Corresponds to the binary report file in the form of memory, that is, the corresponding *.jasper file.

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

When a report has been loaded with data, the document appears as an instance of the Jasperprint class. This class can be viewed directly with the Jasperreport built-in viewer, or it can be serialized to a hard disk for later use, or sent to the Internet. The example of this class is the product of the report filling process, which can be exported to various popular formats such as Pdf,html,xml and so on by the export method in the Jasperreport class library.

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

Loaders are used for each major stage of report generation, such as compiling, populating, and so on. This class can be used by both the user and the engine to load the required serialized objects such as file, URLs, Intput Stream, and so on. The most interesting function of this class is loadonjectfromlocation (String location) [3]. When the user uses this class to load an object from a specified location, the function will first interpret it as a valid URL, and if the parse fails, the function will assume that the provided position is a file name on the hard disk and will attempt to read it. If the file is not found at the specified location, it will locate a resource corresponding to that position through classpath, and will throw an exception after all efforts fail.

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

This is a compilation-related class, using some of the compilation methods it provides, allowing us to compile a report design document (*.jrxml file) into a binary file (*.jasper file). In addition, it can also direct the net.sf.jasperreports.engine.design.JasperDesign (*. The Jrxml file is compiled into an Net.sf.jasperreports.engine.JasperReport object in in-memory form.

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

This class is used to populate the data for the report. A report fill is a database connection for a report's data query, setting values for the report's parameters, and so on. Fills the *.jasper file before it is populated and becomes the *.jrprint file-a file that can be displayed or exported as a report. This class provides many ways to accept various types of report design, which can be an object, a file, or an input stream. It also outputs a variety of results: file, Object, Output Stream.

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

Used to finish printing a *.jrprint file. In Japserreports, we can use this class to print the report, which contains all the printing functions. It provides a way to print the entire document or part of a document, without displaying the Print dialog box. Use this class 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 different places (files, input and output streams, etc.). Used to export a report that can be displayed to a report file in a variety of formats, such as PDF, HTML, XML, and other formats.

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

This is a series of file exporters that are used to export the *.jrprint file to a corresponding format of the report file. Examples include XSL, PDF, HTML, XML, CSV, RTF, TXT, and other formats. Jrxlsexporter, Jrpdfexporter, Jrxmlexporter, Jrcsvexporter, Jrhtmlexporter, Jrtextexporter, JRRtfExporter.

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

This class can directly export the *.jasper file to a report file in various formats. Sometimes we do not want to generate intermediate Net.sf.jasperreports.engine.JasperPrint objects in the report filling process, but directly generate the document format we need, for example: PDF or HTML.

5. Report Data source

The Jasper report engine obtains data from a data source, which can be obtained from databases, XML files, object arrays, and objects in the collection. Earlier we introduced the use of Jasperfillmanager to achieve the data fill of the report, specifically by the Fillreportxxx () method to get the data source to fill.

(1) JDBC Data source

The Jrresultsetdatasource class is associated into a Java.sql.ResultSet object. This is when report data extracts the most common data source implementations from the relational database. If you use Java.sql.Connection to pass to the engine, it first executes the related query and returns the object in Java.sql.ResultSet in a Jrresultsetdatasource instance.

(2) JavaBean data source

The Jrbeanarraydatasource class and the Jrbeancollectiondatasource class implement an array or collection of JavaBean objects that can be individually wrapped. Each object in an array or collection is treated as a record in this type of data source. The mapping between a specific JavaBean property and the corresponding report field is done through a naming convention. The name of the report field must be the same as the name of the specified JavaBeans specification JavaBean property.

(3) based on the map data source

The Java.util.Map object that the parent application has stored in memory provides the implementation class Jrmaparraydatasource and Jrmapcollectiondatasource that populate the data very useful. Each mapped object in the wrapped array or collection is considered to be a virtual record in the data source, and the value of each report field is extracted from the map using the report field name as the key.

(4) TableModel data source

Data for many client applications is displayed in tabular format. A common requirement in many applications is to allow users to print reports in the form of that form. The implementation class Jrtablemodeldatasource the tasks that are reported by the swing application that produces the tabular format. This class encapsulates a Javax.swing.table.TableModel object. The TableModel objects that are listed in the wrapper can be accessed by their name or their 0-based index.

(5) XML data source

Class Jrxmldatasource is based on the DOM, which uses an XPath expression to select the implementation of the data source for XML document data. A record in an XML data source is represented by a node element that is selected through an XPath expression. The 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 a data source that retrieves its data from a structured text file, typically a CSV. The field values are retrieved using their column index.

(7) XLS data source

The jrxlsdatasource represents the implementation of the data source for the data that it retrieves for the Excel file. The implementation of the report field mapping for this data source is also based on the Field column index.

(8) Empty data sources

Class Jremptydatasource, which simulates a given number of data sources with an internal virtual empty record. It is provided by the user Interface tool to provide basic report preview functionality, or in special report templates, or for testing and debugging purposes.

So far, Jasperreport report development of the entire process and implementation of the principle has been introduced, after understanding these, after the practice of the heart will be clear.

Reference documents

[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/

Jasperreport Report Development (i)--Principle Introduction

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.