Developing a Web Project for JasperReports

0
192
11 min read

(For more resources related to this topic, see here.)

Setting the environment

First, we need to install the required software, Oracle Enterprise Pack for Eclipse 12c, from http://www.oracle.com/technetwork/middleware/ias/ downloads/wls-main-097127.html using Installers with Oracle WebLogic Server, Oracle Coherence and Oracle Enterprise Pack for Eclipse, and download the Oracle Database 11g Express Edition from http://www.oracle.com/technetwork/products/express-edition/overview/index.html. Setting the environment requires the following tasks:

  • Creating database tables

  • Configuring a data source in WebLogic Server 12c

  • Copying JasperReports required JAR files to the server classpath

First create a database table, which shall be the data source for creating the reports, with the following SQL script. If a database table has already been created, the table may be used for this article too.

CREATE TABLE OE.Catalog(CatalogId INTEGER PRIMARY KEY, Journal
VARCHAR(25), Publisher VARCHAR(25),Edition VARCHAR(25), Title
Varchar(45), Author Varchar(25));
INSERT INTO OE.Catalog VALUES('1', 'Oracle Magazine', 'Oracle
Publishing', 'Nov-Dec 2004', 'Database Resource Manager', 'Kimberly
Floss');
INSERT INTO OE.Catalog VALUES('2', 'Oracle Magazine', 'Oracle
Publishing', 'Nov-Dec 2004', 'From ADF UIX to JSF', 'Jonas Jacobi');
INSERT INTO OE.Catalog VALUES('3', 'Oracle Magazine', 'Oracle
Publishing', 'March-April 2005', 'Starting with Oracle ADF ', 'Steve
Muench');

Next, configure a data source in WebLogic server with JNDI name jdbc/OracleDS. Next, we need to download some JasperReports JAR files including dependencies. Download the JAR/ZIP files listed below and extract the zip/tar.gz to a directory, c:/jasperreports for example.

  JAR/ZIP

Donwload URL

jasperreports-4.7.0.jar

http://sourceforge.net/projects/

jasperreports/files/jasperreports/JasperReports%204.7.0/

itext-2.1.0

http://mirrors.ibiblio.org/pub/mirrors/maven2/com/

lowagie/itext/2.1.0/itext-2.1.0.jar

commons-beanutils-1.8.3-bin.zip

http://commons.apache.org/beanutils/download_beanutils.cgi

commons-digester-2.1.jar

http://commons.apache.org/digester/download_digester.cgi

commons-logging-1.1.1-bin

http://commons.apache.org/logging/download_logging.cgi

 poi-bin-3.8-20120326 zip or tar.gz

http://poi.apache.org/download.html#POI-3.8

All the JasperReports libraries are open source. We shall be using the following JAR files to create a JasperReports report:

JAR File

Description

commons-beanutils-1.8.3.jar

JavaBeans utility classes

commons-beanutils-bean-collections-1.8.3.jar

Collections framework extension classes

commons-beanutils-core-1.8.3.jar

JavaBeans utility core classes

commons-digester-2.1.jar

Classes for processing XML documents.

commons-logging-1.1.1.jar

Logging classes

iText-2.1.0.jar

PDF library

jasperreports-4.7.0.jar

JasperReports API

  • poi-3.8-20120326.jar,
  • poi-excelant-3.8-20120326.jar,
  • poi-ooxml-3.8-20120326.jar,
  • poi-ooxml-schemas-3.8-20120326.jar,
  • poi-scratchpad-3.8-20120326.jar

Apache Jakarta POI  classes and dependencies.

Add the Jasper Reports required by the JAR files to the user_projectsdomains base_domainbinstartWebLogic.bat script’s CLASSPATH variable:

set SAVE_CLASSPATH=%CLASSPATH%;C:jasperreportscommonsbeanutils-
1.8.3commons-beanutils-1.8.3.jar;C:jasperreportscommonsbeanutils-
1.8.3commons-beanutils-bean-collections-1.8.3.jar;C:
jasperreportscommons-beanutils-1.8.3commons-beanutils-core-
1.8.3.jar;C:jasperreportscommons-digester-2.1.jar;C:jasperreports
commons-logging-1.1.1commons-logging-1.1.1.jar;C:jasperreports
itext-2.1.0.jar;C:jasperreportsjasperreports-4.7.0.jar;C:
jasperreportspoi-3.8poi-3.8-20120326.jar;C:jasperreportspoi-
3.8poi-scratchpad-3.8-20120326.jar;C:jasperreportspoi-3.8poiooxml-
3.8-20120326.jar;C:jasperreportspoi-3.8.jar;C:jasperreports
poi-3.8poi-excelant-3.8-20120326.jar;C:jasperreportspoi-3.8poiooxml-
schemas-3.8-20120326.jar

Creating a Dynamic Web project in Eclipse

First, we need to create a web project for generating JasperReports reports.

Select File | New | Other. In New wizard select Web | Dynamic Web Project. In Dynamic Web Project configuration specify a Project name (PDFExcelReports for example), select the Target Runtime as Oracle WebLogic Server 11g R1 ( 10.3.5). Click on Next.

Select the default Java settings; that is, Default output folder as build/classes, and then click on Next. In WebModule, specify ContextRoot as PDFExcelReports and Content Directory as WebContent. Click on Finish. A web project for PDFExcelReports gets generated. Right-click on the project node in ProjectExplorer and select Project Properties. In Properties, select Project Facets. The Dynamic Web Module project facet should be selected by default as shown in the following screenshot:

Next, create a User Library for JasperReports JAR files and dependencies. Select Java Build Path in Properties. Click on Add Library. In Add Library, select User Library and click on Next. In User Library, click on User Libraries. In User Libraries, click on New. In New User Library, specify a User library name (JasperReports) and click on OK. A new user library gets added to User Libraries. Click on Add JARs to add JAR files to the library. The following screenshot shows the JasperReports that are added:

Creating the configuration file

We require a JasperReports configuration file for generating reports. JasperReports XML configuration files are based on the jasperreport.dtd DTD, with a root element of jasperReport. We shall specify the JasperReports report design in an XML configuration bin file, which we have called config.xml. Create an XML file config.xml in the webContent folder by selecting XML | XML File in the New wizard.

Some of the other elements (with commonly used subelements and attributes) in a JasperReports configuration XML file are listed in the following table:

XML Element

Description

Sub-Elements

Attributes

jasperReport

Root Element

reportFont, parameter, queryString, field, variable, group, title, pageHeader, columnHeader, detail, columnFooter, pageFooter.

name, columnCount, pageWidth, pageHeight, orientation, columnWidth, columnSpacing, leftMargin, rightMargin, topMargin, bottomMargin.

reportFont

Report level font definitions

name, isDefault, fontName, size, isBold, isItalic, isUnderline, isStrikeThrough, pdfFontName, pdfEncoding, isPdfEmbedded

parameter

Object references used in generating a report. Referenced with P${name}

parameterDescription, defaultValueExpression

name, class

queryString

Specifies the SQL query for retrieving data from a database.

field

Database table columns included in report. Referenced with F${name}

fieldDescription

name, class

variable

Variable used in the report XML file. Referenced with V${name}

variableExpression, initialValueExpression

name,class.

title

Report title

band

pageHeader

Page Header

band

columnHeader

Specifies the different columns in the report generated.

band

detail

Specifies the column values

band

columnFooter

Column footer

band

A report section is represented with the band element. A band element includes staticText and textElement elements. A staticText element is used to add static text to a report (for example, column headers) and a textElement element is used to add dynamically generated text to a report (for example, column values retrieved from a database table). We won’t be using all or even most of these element and attributes. Specify the page width with the pageWidth attribute in the root element jasperReport. Specify the report fonts using the reportFont element. The reportElement elements specify the ARIAL_NORMAL, ARIAL_BOLD, and ARIAL_ITALIC fonts used in the report. Specify a ReportTitle parameter using the parameter element. The queryString of the example JasperReports configuration XML file catalog.xml specifies the SQL query to retrieve the data for the report.

<queryString><![CDATA[SELECT CatalogId, Journal,
Publisher, Edition, Title,
Author FROM OE.Catalog]]> </queryString>

The PDF report has the columns CatalogId, Journal, Publisher, Edition, Title, and Author. Specify a report band for the report title. The ReportTitle parameter is invoked using the $P {ReportTitle} expression. Specify a column header using the columnHeader element. Specify static text with the staticText element. Specify the report detail with the detail element. A column text field is defined using the textField element. The dynamic value of a text field is defined using the textFieldExpression element:

<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Cata
logId}]]></textFieldExpression>
</textField>

Specify a page footer with the pageFooter element. Report parameters are defined using $P{}, report fields using $F{}, and report variables using $V{}. The config. xml file is listed as follows:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//
EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="PDFReport" pageWidth="975">

The following code snippet specifies the report fonts:

<reportFont name="Arial_Normal" isDefault="true" fontName="Arial"
size="15" isBold="false" isItalic="false" isUnderline="false"
isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252"
isPdfEmbedded="false"/>
<reportFont name="Arial_Bold" isDefault="false" fontName="Arial"
size="15" isBold="true" isItalic="false" isUnderline="false"
isStrikeThrough="false" pdfFontName="Helvetica-Bold"
pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<reportFont name="Arial_Italic" isDefault="false" fontName="Arial"
size="12" isBold="false" isItalic="true" isUnderline="false"
isStrikeThrough="false" pdfFontName="Helvetica-Oblique"
pdfEncoding="Cp1252" isPdfEmbedded="false"/>

The following code snippet specifies the parameter for the report title, the SQL query to generate the report with, and the report fields. The resultset from the SQL query gets bound to the fields.

<parameter name="ReportTitle" class="java.lang.String"/>
<queryString><![CDATA[SELECT CatalogId, Journal, Publisher, Edition,
Title, Author FROM Catalog]]></queryString>
<field name="CatalogId" class="java.lang.String"/>
<field name="Journal" class="java.lang.String"/>
<field name="Publisher" class="java.lang.String"/>
<field name="Edition" class="java.lang.String"/>
<field name="Title" class="java.lang.String"/>
<field name="Author" class="java.lang.String"/>

Add the report title to the report as follows:

<title>
<band height="50">
<textField>
<reportElement x="350" y="0" width="200" height="50" />
<textFieldExpression class="java.lang.
String">$P{ReportTitle}</textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band>
</band>
</pageHeader>

Add the column’s header as follows:

<columnHeader>
<band height="20">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[CATALOG ID]]></text>
</staticText>
<staticText>
<reportElement x="125" y="0" width="100" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[JOURNAL]]></text>
</staticText>
<staticText>
<reportElement x="250" y="0" width="150" height="20"/>
<textElement> <font isUnderline="false"
reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[PUBLISHER]]></text>
</staticText>
<staticText>
<reportElement x="425" y="0" width="100" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[EDITION]]></text>
</staticText>
<staticText>
<reportElement x="550" y="0" width="200" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[TITLE]]></text>
</staticText>
<staticText>
<reportElement x="775" y="0" width="200" height="20"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Bold"/>
</textElement>
<text><![CDATA[AUTHOR]]></text>
</staticText>
</band>
</columnHeader>

The following code snippet shows how to add the report detail, which consists of values retrieved using the SQL query from the Oracle database:


<detail>
<band height="20">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Cata
logId}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="125" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Jour
nal}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="250" y="0" width="150" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Publ
isher}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="425" y="0" width="100" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Edit
ion}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="550" y="0" width="200" height="20"/>
<textFieldExpression class="java.lang.
String"><![CDATA[$F{Title}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="775" y="0" width="200" height="20"/>
<textFieldExpression class="java.lang.
String"><![CDATA[$F{Author}]]></textFieldExpression>
</textField>
</band>
</detail>

Add the column and page footer including the page number as follows:

<columnFooter>
<band>
</band>
</columnFooter>
<pageFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="40" height="15"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Italic"/>
</textElement>
<text><![CDATA[Page #]]></text>
</staticText>
<textField>
<reportElement x="40" y="0" width="100" height="15"/>
<textElement>
<font isUnderline="false" reportFont="Arial_Italic"/>
</textElement>
<textFieldExpression class="java.lang.
Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band>
</band>
</summary>
</jasperReport>

We need to create a JAR file for the config.xml file and add the JAR file to the WebLogic Server’s domain’s lib directory. Create a JAR file using the following command from the directory containing the config.xml as follows:

>jar cf config.jar config.xml

Add the config.jar file to the user_projectsdomainsbase_domainlib directory, which is in the classpath of the server.

LEAVE A REPLY

Please enter your comment!
Please enter your name here