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

Elements of a JRXML report template

In the previous example, we used the <detail> element of the JRXML report template to generate a report displaying some static text. The <detail> element is used to display the main section of the report. However, JRXML templates can contain many other sections that allow us to display secondary data on the report or to do some other tasks, such as importing Java packages and controlling how the data is displayed in the report.

The following sections cover all the subelements of the <jasperReport> root element. Unless stated otherwise, each element can be used any number of times in the template.

<property>

This element is used for putting arbitrary information in the report template.

<property name="someproperty" value="somevalue" />

Properties can be retrieved by a Java application that is loading the report by invoking the JasperReport.getProperty() method.

<import>

This element is used for importing individual Java classes or complete packages.

<import value="java.util.HashMap" />

<template>

The report styles can be defined in separate report templates to allow these styles to be reused across the reports. This mechanism is similar to the way cascading stylesheets can be defined in separate CSS files when dealing with HTML. Report style templates can be defined in XML files, which are conventionally saved in JRTX files or, more infrequently, in an instance of a class implementing the net.sf.jasperreports.engine.JRTemplate interface.

<template>"my_template.jrtx"</template>

<style>

This element is used for styling report elements, setting the font style, size, background color, foreground color, and so on. Most other report elements have a style attribute that can be used to specify their style. The <style> element has an isDefault attribute that can be used to specify that the style being defined is the default style, and should be used when other elements don't specify their style attribute.

<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="10"isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false"/>

<subDataset>

The <subDataset> element can be used to provide data indirectly in the report to charts and crosstabs in the report template.

<subDataset name="Client_Data"> 
  <parameter name="Client" class="java.lang.String"/> 
  <queryString>
    <![CDATA[SELECT foo, bar, temp 
             FROM some_table 
             WHERE client_code = $P{Client}]]>
  </queryString> 
  <field name="foo" class="java.lang.String"/> 
  <field name="bar" class="java.lang.String"/> 
  <field name="temp" class="java.lang.String"/> 
</subDataset> 

Subdatasets need to be referenced by a crosstab or chart.

<parameter>

This element is used to define report parameters. Parameter values are supplied through a java.util.Map parameter by calling the appropriate methods in the JasperReports API.

<parameter name="SomeParameter"class="java.lang.String"/>

<queryString>

This element is used to define an SQL query to obtain data from a database.

<queryString>
  <![CDATA[SELECT column_name FROM table_name]]>
</queryString>

A JRXML template can contain zero or one <queryString> element. This element is required if we wish to embed an SQL query in the report template.

<field>

This element is used to map data from datasources or queries to report templates. Fields can be combined in report expressions to obtain the necessary output.

<field name="FieldName" class="java.lang.String"/>

<sortField>

This element is used to sort the data in the report by the field specified in this element's name attribute. Sorting can be ascending or descending, as specified in the order attribute. If no order is specified, the default is ascending.

<sortField name="BirthDate" order="Descending"/>

A JRXML template can have one or more <sortField> elements corresponding to fields in the report template.

<variable>

The report expressions used several times in a report can be assigned to the variables to simplify the template.

<variable name="VariableName"
          class="java.lang.Double"
          calculation="Sum">
    <variableExpression>
      $F{FieldName}
    </variableExpression>
</variable>

<filterExpression>

This element is used to filter out the datasource records from the report.

<filterExpression>
  <![CDATA[$F{status}.equals("active") ? Boolean.TRUE :Boolean.FALSE]]>
</filterExpression> 

If the expression nested inside the <filterExpression> element resolves to Boolean.TRUE, the current row in the datasource is included in the report. If it resolves to Boolean.FALSE or null, the current row is not included in the datasource. Please note that this element is primarily meant to be be used when our datasource type cannot be filtered trivially, such as when we use a CSV file datasource.

A report template can contain zero or one <filterExpression> element.

<group>

This element is used to group the consecutive records in a datasource that share some common characteristics.

<group name="GroupName">
  <groupExpression>
    <![CDATA[$F{FieldName}]]>
  </groupExpression>
</group>

<background>

This element is used to define the page background for all the pages in the report. It can be used to display images or text and is very useful to display watermarks.

<background>
  <band height="745">
    <image scaleImage="Clip"
           hAlign="Left" vAlign="Bottom">
       <reportElement x="0" y="0"
        width="160" height="745"/>
       <imageExpression>"image.gif"
       </imageExpression>
      </image>
  </band>
</background>

This element cannot be used more than once in a JRXML template.

<title>

This is the report title. It appears only once at the beginning of the report.

<title>
  <band height="50">
    <staticText>
      <reportElement x="180" y="0"width="200" height="20"/>
      <text><![CDATA[Title]]></text>
    </staticText>
  </band>
</title>

<pageHeader>

This element defines a page header that is printed at the beginning of every page in the report.

<pageHeader>
  <band height="20">
    <staticText>
      <reportElement x="180" y="30"
                     width="200" height="20"/>
          <text>
            <![CDATA[Page Header]]>
      </text>
    </staticText>
  </band>
</pageHeader>

A JRXML template can contain zero or one <pageHeader> element.

<columnHeader>

This element defines the contents of column headers. It is ignored if the report has a single column.

<columnHeader>
  <band height="20">
    <staticText>
      <reportElement x="180" y="50"
                     width="200" height="20"/>
        <text>
          <![CDATA[Column Header]]>
        </text>
    </staticText>
  </band>
</columnHeader>

If present, the number of <columnHeader> elements in the template must match the number of columns.

<detail>

This element defines the detail section of the report. The content of the <detail> section is repeated for each record in the report's datasource.

<detail>
  <band height="20">
    <textField>
      <reportElement x="10" y="0"
         width="600" height="20" />
        <textFieldExpression class="java.lang.String">
              <![CDATA[$F{FieldName}]]>
        </textFieldExpression>
    </textField>
  </band>
</detail>

A JRXML template can contain zero or one <detail> elements. Most report templates contain a <detail> element; typically, this is where the main data of the report is displayed.

<columnFooter>

This element defines the contents of column footers. It is ignored if the report has a single column.

<columnFooter>
  <band height="20">
    <staticText>
      <reportElement x="0" y="0"
                     width="200" height="20"/>
          <text>
            <![CDATA[Column Footer]]>
          </text>
    </staticText>
  </band>
</columnFooter>

A JRXML template can contain zero or more <columnFooter> elements. If present, the number of <columnFooter> elements in the template must match the number of columns.

<pageFooter>

This element defines a page footer that is printed at the bottom of every page in the report.

<pageFooter>
  <band height="20">
    <staticText>
      <reportElement  x="0" y="5"
                      width="200" height="20"/>
      <text>
        <![CDATA[Page Footer]]>
      </text>
    </staticText>
  </band>
</pageFooter>

A JRXML template can contain zero or one <pageFooter> element.

<lastPageFooter>

Data defined in this element is displayed as the page footer of the last page rather than the footer defined in the <pageFooter> element.

<lastPageFooter>
  <band height="20">
    <staticText>
      <reportElement x="0" y="5"
                     width="200" height="20"/>
      <text>
        <![CDATA[Last Page Footer]]>
      </text>
    </staticText>
  </band>
</lastPageFooter>

A JRXML template can contain zero or one <lastPageFooter> element.

<summary>

This element is printed once at the end of the report.

<summary>
  <band height="20">
    <staticText>
      <reportElement x="0" y="5"
                     width="200" height="20"/>
      <text>
        <![CDATA[Summary]]>
      </text>
    </staticText>
  </band>
</summary>

A JRXML template can contain zero or one <summary> element.

<noData>

The <noData> element can be used to control what will be generated in the report when the datasource contains no data.

<noData>
  <band height="20">
    <staticText>
      <reportElement x="0" y="5"
                     width="200" height="20"/>
      <text>
        <![CDATA[No data found]]>
      </text>
    </staticText>
  </band>
</noData>

Just like the <detail> element, most elements discussed in the previous sections contain a single <band> element as its only child element. We will discuss the specific subelements of the <band> element in later chapters.

In the following screenshot, we can see a report that can help us visualize the relative position of the report sections:

<noData>

As we can see, the page footer is labeled Page Footer/Last Page Footer. If the JRXML template for the report contains a <lastPageFooter> element, its contents will be displayed in the last page of the report, instead of the contents of the <pagefooter> element. It is worth mentioning that if our report has only one page, and the report template contains both the <pageFooter> and the <lastPageFooter> elements, then in that case the contents of <lastPageFooter> will be displayed as the footer of the first (and only) page; the value of the <pageFooter> element will never be displayed.

Before we move on, we should mention that the <columnHeader> and <columnFooter> elements will be displayed on the report only if it has more than one column. How to add columns to a report is discussed in detail in Chapter 6, Report Layout and Design.

主站蜘蛛池模板: 辉南县| 乳山市| 财经| 凉城县| 海兴县| 北流市| 霍邱县| 治多县| 信宜市| 唐海县| 余干县| 大名县| 平和县| 陆河县| 肥乡县| 鄂托克前旗| 吉木乃县| 阳山县| 库尔勒市| 农安县| 西昌市| 景泰县| 开鲁县| 东港市| 光泽县| 嘉荫县| 桐庐县| 宜春市| 松江区| 通辽市| 加查县| 和平县| 阳信县| 界首市| 微博| 霞浦县| 综艺| 灵台县| 固镇县| 巩留县| 常德市|