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 following figure 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]
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.