Report Scriptlets

JasperReports Tutorial

We saw in our previous chapters, data displayed on the report is usually fetched from report parameters and report fields. This data can be processed using the report variables and their expressions. There are situations when a complex functionality cannot be achieved easily using report expressions or variables. Examples of this may be complex String manipulations, building of Maps or Lists of objects in memory or manipulations of dates using 3rd party Java APIs. For such situations, JasperReports provides us with a simple and powerful means of doing this with Scriptlets .

Scriptlets are sequences of Java code that are executed every time a report event occurs. Values of report variables can be affected through scriptlets.

Scriptlet Declaration

We can declare a scriptlet in two ways:

Using <scriptlet> element. This element has name attribute and class attribute. The class attribute should specify the name of the class, which extends JRAbstractScriptlet class. The class must be available in the classpath at report filling time and must have an empty constructor, so that the

engine can instantiate it on the fly.

Using the attribute scriptletClass of the element <jasperReport>. in the report template (JRXML). By setting this attribute with fully qualified name of scriptlet (including the entire package name), we indicate that we want to use a scriptlet. The scriptlet instance created with this attribute, acts like the first scriptlet in the list of scriptlets and has the predefined name REPORT.

Scriptlet class

A scriptlet is a java class which must extend either of the following classes:

net.sf.jasperreports.engine.JRAbstractScriptlet. This class contains a number of abstract methods that must be overridden in every implementation. These methods are called automatically by JasperReports at the appropriate moment. Developer must implement all the abstract methods.

net.sf.jasperreports.engine.JRDefaultScriptlet. This class contains default empty implementations of every method in JRAbstractScriptlet. A developer is only required to implement those methods he/she needs for their project.

Following table lists the methods in the above class. These methods will be called by the report engine at the appropriate time, during report filling phase.

Source: www.tutorialspoint.com

Category: Bank

Similar articles: