- 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.
- Core Data iOS Essentials
- Entity Framework Tutorial
- 穿越Photoshop CC
- UG NX 完全實例解析
- After Effects CS6入門與提高
- Lighttpd
- Liferay Portal Systems Development
- AutoCAD 2016入門與提高(超值版)
- Plone 3 Intranets
- Photoshop移動UI設計從入門到精通
- 中文版Illustrator 2020基礎教程
- 剪映短視頻剪輯從入門到精通:宣傳短片+電商視頻+產品廣告+活動慶典
- Flash CC動畫制作與應用(第3版)
- Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
- Adobe Flash 11 Stage3D (Molehill) Game Programming Beginner's Guide