Chapter 6. Report Structure

This chapter describes the report structure, available report objects and their properties. This is basically the JasperReports "Quick Reference" presented in the context of the JasperAssistant's user interface.

6.1. Expressions

Before the report structure can be presented, the notion of expressions must be introduced. Expressions are the core feature of JasperReports. It is an important mechanism that allows to manipulate and display report data, perform various calculations, and customize the aspects and the visibility of report objects.

Example 6.1. A typical expression

$F{LastName} + " " + $F{LastName}

A JasperReports expression is basically a Java expression plus some additional syntax that allows to reference parameters, variables and fields. To refer to a parameter named DATA, the syntax $P{PARAM_NAME} is used. For variables and fields the syntax is $V{VAR_NAME} and $F{FIELD_NAME} respectively.

Tip

Since JasperAssistant 1.5.3/JasperReports 0.6.6 Groovy is supported as an alternative scripting language that can be used in report expressions. Groovy is a scripting language for the Java 2 Platform that has many of the features that people like so much in languages like Python, Ruby and Smalltalk, making them available to Java developers using a Java-like syntax. Groovy greatly simplifies the syntax of report expressions by eliminating most of the syntax overhead of Java language. Note that additional syntax ($P, $V and $F) remains the same when using Groovy. For more information on the syntax of Groovy language please visit:

http://groovy.codehaus.org/Language+Guide

Expression syntax

$F{FIELD}

Refer to the field named FIELD.

$V{VAR}

Refer to the variable named VAR.

$P{PARAM}

Refer to the parameter named PARAM. Can be used to add parameterize the report's query.

$P!{PARAM}

Refer to the parameter named PARAM. This is a special syntax that can be only used in report's query. It allows to insert the parameter's content into the query string. For example it can be used to create a query with dynamic WHERE clause specified through a parameter.

$R{keyName}

Retrieve a string using the key keyName from the resource bundle associated with report.

Keep in mind that expressions are written in Java or Java-like Groovy. This means that all the power of Java is at your disposal. The possibility of making method calls alone, allows to construct expressions of practically unlimited complexity. Note that the result of an expression is always an object.

Example 6.2. More expressions

new Integer(Math.max($V{Price1}, $V{Price2}))
(new SimpleDateFormat("dd/MM/yyyy")).format($F{OrderDate})
$F{SpecialOffer}.booleanValue() ? $F{SpecialPrice} : $F{Price}

6.1.1. Built-in Functions

JasperReports provides a small set of built-in functions that can be used in report expressions. This functions can be though as built-in utilities that allow to perform common operations.

Note

At this moment the set of available functions is limited, but it can be extended in the future. If you have suggestions for new built-in functions, please post a request on JasperReports Open Discussion forum. If approved, the new function will be included in the next version of JasperReports. The forum can be found here:

http://sourceforge.net/forum/forum.php?forum_id=113529

Example 6.3. Using built-in functions

msg("Total cost is {0}", $F{TOTAL})
msg("Matched {0} products out of {1}",
                    $F{MATCHED}, $P{TOTAL})

Table 6.1. Built-in Functions

SignatureDescription
String str(String key); Gets a string for the given key from the resource bundle associated with the report. The use of this function is equivalent to the use of $R{key} syntax.
String msg(String pattern,
           Object arg0);

Creates a java.util.MessageFormat with the given pattern and uses it to format the given arguments. For more information, refer to the MessageFormat class documentation.

String msg(String pattern,
           Object arg0,
           Object arg1);

Same as the above

String msg(String pattern,
           Object arg0,
           Object arg1,
           Object arg2);

Same as the above