- JasperReports 3.5 for Java Developers
- David R. Heffelfinger
- 662字
- 2021-04-01 13:58:23
Creating a JRXML report template
When creating a report, the first step is to create a JRXML template. As we mentioned in Chapter 1, An Overview of JasperReports, JasperReports JRXML templates are standard XML files but, by convention, they have an extension of .jrxml
and are referred to as JRXML files or JRXML templates. JRXML templates can be written by hand, alternatively, a visual report template generator can be used. The most popular JRXML report template generator is iReport. We will cover iReport in Chapter 10, Graphical Report Design with iReport.
All JRXML files contain a root <jasperReport>
element. The <jasperReport>
root element can contain many subelements, and all of these subelements are optional. Our goal for this chapter is to get a feel of how to design a report, so we will avert most of the <jasperReport>
subelements. We will use only one subelement, namely the <detail>
subelement.
Our first report will display a static string. Its JRXML is as follows:
<?xml version="1.0"?> <jasperReportxmlns="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="FirstReport"> <detail> <band height="20"> <staticText> <reportElement x="20" y="0" width="200" height="20"/> <text> <![CDATA[If you don't see this, it didn't work]]> </text> </staticText> </band> </detail> </jasperReport>
There are some elements in the above JRXML file that we haven't seen before, such as the following:
We have seen the <band>
element in the previous examples. The <detail>
element can contain only a single <band>
element as its only subelement. The <band>
element can contain many different elements that can be used to display text, charts, images, or geometric figures. The above example contains a single <staticText>
element.
Previewing the XML report template
JasperReports includes a utility that can be used to preview report designs. This utility makes designing reports much faster. We can immediately preview a report design without having to compile or fill it.
The utility is a standalone Java application included in the JasperReports JAR file. The class that needs to be executed is net.sf.jasperreports.view.JasperDesignViewer
. The easiest way to execute this class is to use an ANT target to execute it, including all the required libraries in the CLASSPATH. This is the approach that is used in the JasperReports samples included in the project JAR file, and it is the approach we will take. The following ANT build file will launch the JasperDesignViewer to preview our report:
<project name="FirstReport XML Design Preview" default="viewDesignXML"basedir="."> <description> Previews our First Report XML Design </description> <property name="file.name" value="FirstReport" /> <!-- Directory where the JasperReports project file was extracted,needs to be changed to match the local environment --> <property name="jasper.dir" value="/opt/jasperreports-3.5.2"/> <property name="classes.dir" value="${jasper.dir}/build/classes" /> <property name="lib.dir" value="${jasper.dir}/lib" /> <path id="classpath"> <pathelement location="./"/> <pathelement location="${classes.dir}" /> <fileset dir="${lib.dir}"> <include name="**/*.jar"/> </fileset> </path> <target name="viewDesignXML"description="Launches the design viewer to preview the XMLreport design."> <java classname="net.sf.jasperreports.view.JasperDesignViewer"fork="true"> <arg value="-XML"/> <arg value="-F${file.name}.jrxml"/> <classpath refid="classpath"/> </java> </target> </project>
This ANT build file must be saved in the same directory as our JRXML file. It is recommended that the JRXML file be saved with the report name as its filename. The report name is defined in the root <jasperReport>
element. In this example, we chose to use FirstReport
as the report name; therefore, the recommended filename for this report template is FirstReport.jrxml
.
If we save our ANT build file with the standard name of build.xml
, there is no need to specify the build filename in the command line. The example build file here has one target named viewDesignXML
. As this target is the default target, there is no need to specify it in the command line. Typing ant
in the command line will execute the default target, and a preview of our report will be displayed.
$ ant Buildfile: build.xml viewDesignXML:
After executing the viewDesignXML
target, we should see a window labeled JasperDesignViewer displaying our report template preview.

The JasperDesignViewer can be safely terminated by closing the window or by hitting Ctrl+c in the command-line window.
Note
In this particular case, we can see all the text in the preview because this report contains only static text. For reports displaying data coming from datasources or report parameters, the actual text won't be displayed in the report. Report expressions for obtaining the data are displayed instead, as JasperDesignViewer does not have access to the actual datasource or report parameters.
- 虛擬現(xiàn)實:開啟現(xiàn)實與夢想之門
- Wordpress 3 Complete
- AI圖像處理:Photoshop+Firefly后期處理技術基礎與實戰(zhàn)
- 中文版Illustrator CC基礎培訓教程(移動學習版)
- 數(shù)碼攝影后期處理秘笈:Photoshop CC專業(yè)調色(第2版)
- Procreate+ SketchUp +Photoshop建筑設計手繪表現(xiàn)技法
- 中文版Photoshop 2022基礎教程
- 新印象Premiere短視頻拍攝+剪輯+特效關鍵技術
- 中望3D從入門到精通
- 電腦寫作與定制五筆(第2版)
- Oracle Web Services Manager
- 零基礎學Procreate萌物繪
- 3ds Max 2014/VRay效果圖制作實戰(zhàn)從入門到精通
- SQL Server 2005數(shù)據(jù)挖掘與商業(yè)智能完全解決方案
- Adobe創(chuàng)意大學視頻編輯師 After Effects CS5 + Premiere Pro CS5 標準實訓教材