官术网_书友最值得收藏!

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:

  • <staticText> defines static text that does not depend on any datasources, variables, parameters, or report expressions
  • <reportElement> defines the position and width of the <staticText> element
  • <text> defines the actual static text that is displayed on the report

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.

Note

<reportElement> is a required element of not only the <staticText> element, but also of all the subelements of the <band> element. The x and y coordinates defined in <reportElement> are relative to the band containing its parent element (<staticText> in this example).

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.

Previewing the XML report template

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.

主站蜘蛛池模板: 时尚| 双辽市| 江源县| 襄垣县| 兴业县| 南京市| 托里县| 兴仁县| 海盐县| 盈江县| 大宁县| 郧西县| 榆中县| 安远县| 思茅市| 台湾省| 英吉沙县| 胶南市| 平昌县| 临洮县| 辽阳县| 全椒县| 塔河县| 常州市| 周口市| 乐安县| 昌乐县| 上虞市| 黄大仙区| 林芝县| 兴仁县| 三穗县| 镇赉县| 习水县| 鄱阳县| 巍山| 马山县| 信阳市| 米脂县| 繁昌县| 水城县|