JasperReports defines a report with an XML file. A jrxml file is composed of a set of sections; some concerned with the report’s physical characteristics (such as the dimensions of the page, positioning of the fields, and height of the bands), and some concerned with the logical characteristics (such as the declaration of the parameters and variables and the definition of a query for data selection).
The Report Lifecycle
The life cycle of a JasperReport is divided into two phases:
• | Report development – designing and planning the report, creating a JRXML file, and compiling a Jasper file from the JRXML. |
• | Report execution – loading the Jasper file, filling the report, and exporting the output (a Jasper print object) in a final format. |
Jaspersoft Studio is primarily focused on report development, though it is able to preview the result and export it in all the supported formats. Jaspersoft Studio provides support for a wide range of data sources and allows users to create custom data sources, thereby becoming a complete environment for report development and testing.
When you design a report, you specify where the data comes from, how it is positioned on the page, and additional functionality, such as parameters for input controls or complex formulas to perform calculations. The result is a template, similar to a form containing blank space, that is filled with data when the report is executed. The template is stored in a JRXML file, which is an XML document that contains the definition of the report layout and design.
Before executing a report, the JRXML must be compiled in a binary object called a Jasper file. Jasper files are what you need to ship with your application in order to run the reports.
Report execution is performed by passing a Jasper file and a data source to JasperReports. There are many data source types. You can fill a Jasper file from an SQL query, an XML file, a .csv file, an HQL (Hibernate Query Language) query, a collection of JavaBeans, and others. If you don't have a suitable data source, JasperReports allows you to write your own custom data source. With a Jasper file and a data source, JasperReports is able to generate the final document in the format you want.
Jaspersoft Studio also lets you configure data sources and use them to test your reports. In many cases, data-driven wizards can help you design your reports much quicker. Jaspersoft Studio includes the JasperReports engine itself to let you preview your report output, test, and refine your reports.
The following table shows sample report source code.
<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="My first report" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <queryString language="SQL"> <![CDATA[select * from address order by city]]> </queryString> <field name="ID" class="java.lang.Integer"> <fieldDescription><![CDATA[]]></fieldDescription> </field> <field name="FIRSTNAME" class="java.lang.String"> <fieldDescription><![CDATA[]]></fieldDescription> </field> <field name="LASTNAME" class="java.lang.String"> <fieldDescription><![CDATA[]]></fieldDescription> </field> <field name="STREET" class="java.lang.String"> <fieldDescription><![CDATA[]]></fieldDescription> </field> <field name="CITY" class="java.lang.String"> <fieldDescription><![CDATA[]]></fieldDescription> </field> [/code]
<group name="CITY"> <groupExpression><![CDATA[$F{CITY}]]></groupExpression> <groupHeader> <band height="27"> <staticText> <reportElement mode="Opaque" x="0" y="0" width="139" height="27" forecolor="#FFFFFF" backcolor="#000000"/> <textElement> <font size="18"/> </textElement> <text><![CDATA[CITY]]></text> </staticText>[/code]
<textField hyperlinkType="None"> <reportElement mode="Opaque" x="139" y="0" width="416" height="27" forecolor="#FFFFFF" backcolor="#000000"/> <textElement> <font size="18" isBold="true"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{CITY}]]> </textFieldExpression> </textField> </band> </groupHeader> <groupFooter> <band height="8"> <line direction="BottomUp"> <reportElement key="line" x="1" y="4" width="554" height="1"/> </line> </band> </groupFooter></group> [/code]
<background> <band/></background> <title> <band height="58"> <line> <reportElement x="0" y="8" width="555" height="1"/> </line> <line> <reportElement positionType="FixRelativeToBottom" x="0" y="51" width="555" height="1"/> </line> <staticText> <reportElement x=”65” y=”13” width ”424” height=”35”/> <textElement textAlignment=”Center”> <font size=”26” isBold=”true”/> </textElement> <text><![CDATA[Classic template]]> </text> </staticText></band></title> [/code]
<pageHeader> <band/> </pageHeader> <columnHeader> <band height="18"> <staticText> <reportElement mode="Opaque" x="0" y="0" width="138" height="18" forecolor="#FFFFFF" backcolor="#999999"/> <textElement> <font size="12"/> </textElement> <text><![CDATA[iD]]></text> </staticText> <staticText> <reportElement mode="Opaque" x="138" y="0" width="138" height="18" forecolor="#FFFFFF" backcolor="#999999"/> <textElement> <font size="12"/> </textElement> <text><![CDATA[FIRSTNAME]]></text> </staticText> <staticText> <reportElement mode="Opaque" x="276" y="0" width="138" height="18" forecolor="#FFFFFF" backcolor="#999999"/> <textElement> <font size="12"/> </textElement> <text><![CDATA[LASTNAME]]></text> </staticText> <staticText> <reportElement mode="Opaque" x="414" y="0" width="138" height="18" forecolor="#FFFFFF" backcolor="#999999"/> <textElement> <font size="12"/> </textElement> <text><![CDATA[sTREET]]></text> </staticText> </band> </columnHeader>[/code]
<detail> <band height="20"> <textField hyperlinkType="None"> <reportElement x="0" y="0" width="138" height="20"/> <textElement> <font size="12"/> </textElement> <textFieldExpression class="java.lang.Integer"><![CDATA[$F{ID}]]> </textFieldExpression> </textField> <textField hyperlinkType="None"> <reportElement x="138" y="0" width="138" height="20"/> </textField> <textElement> <font size="12"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{FIRSTNAME}]]> </textFieldExpression> <textField hyperlinkType="None"> <reportElement x="276" y="0" width="138" height="20"/> <textElement> <font size="12"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{LASTNAME}]]> </textFieldExpression> </textField> <textField hyperlinkType="None"> <reportElement x="414" y="0" width="138" height="20"/> <textElement> <font size="12"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{STREET}]]> </textFieldExpression> </textField> </band> </detail>[/code]
<columnFooter> <band/> </columnFooter> <pageFooter> <band height="26"> <textField evaluationTime="Report" pattern="" isBlankWhenNull="false" hyperlinkType="None"> <reportElement key="textField" x="516" y="6" width="36" height="19" forecolor="#000000" backcolor="#FFFFFF"/> <textElement> <font size="10"/> </textElement>[/code]
<textFieldExpression class="java.lang.String"><![CDATA["" + $V{PAGE_NUMBER}]]></textFieldExpression> </textField> <textField pattern="" isBlankWhenNull="false" hyperlinkType="None"> <reportElement key="textField" x="342" y="6" width="170" height="19" forecolor="#000000" backcolor="#FFFFFF"/> <box> <topPen lineWidth="0.0" line lineColor="#000000"/> <leftPen lineWidth="0.0" line lineColor="#000000"/> <bottomPen lineWidth="0.0" line lineColor="#000000"/> <rightPen lineWidth="0.0" line lineColor="#000000"/> </box> <textElement textAlignment="Right"> <font size="10"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA["Page " + $V{PAGE_NUMBER} + " of "]]></textFieldExpression> </textField>[/code]
<textField pattern="" isBlankWhenNull="false" hyperlinkType="None"> <reportElement key="textField" x="1" y="6" width="209" height="19" forecolor="#000000" backcolor="#FFFFFF"/> <box> <topPen lineWidth="0.0" line lineColor="#000000"/> <leftPen lineWidth="0.0" line lineColor="#000000"/> <bottomPen lineWidth="0.0" line lineColor="#000000"/> <rightPen lineWidth="0.0" line lineColor="#000000"/> </box> <textElement> <font size="10"/> </textElement> <textFieldExpression class="java.util.Date"><![CDATA[new Date()]]> </textFieldExpression> </textField> </band> </pageFooter> <summary> <band/> </summary></jasperReport>[/code]
During compilation of the jrxml file (using some JasperReports classes) the XML is parsed and loaded in a JasperDesign object, which is a rich data structure that allows you to represent the exact XML contents in memory. Regardless of the language used for expressions inside the jrxml, JasperReports creates a special Java class that represents the whole report. The report is then compiled, instanced, and serialized in a jasper file, ready for loading at any time.
JasperReports' speedy operation is due to all of a report’s formulas being compiled into Java-native bytecode and the report structure being verified during compilation instead of at run time. The jasper file contains no extraneous resources, such as images used in the report, resource bundles to run the report in different languages, or extra scriptlets and external style definitions. All these resources must be provided by the host application and located at run time.
Recommended Comments
There are no comments to display.