Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)

JasperReports - JavaScript Sample (version 3.7.6)


Shows how the JavaScript language could be used inside report templates.

Download All Sample Source Files
Browse Sample Source Files on SVN


Main Features in This Sample

Using the JavaScript Language for Report Expressions (JavaScript Report Compiler)

Secondary Features
Report Compilers


top

Using the JavaScript Language for Report Expressions (JavaScript Report Compiler)Documented by Sanda Zaharia


Description / Goal
How to use JavaScript to write report expressions.

Since
3.1.2

Other Samples
/demo/samples/antcompile
/demo/samples/beanshell
/demo/samples/groovy
/demo/samples/java1.5


JavaScript Scripting Example

The main purpose of this sample is to show how the JavaScript compiler implementation works. Useful information about the default JavaScript compiler implementation can be found here.
This sample contains report expressions written using JavaScript. The JavaScriptCompiler default implementation creates the JavaScript-related expression evaluator during the report compilation and prepares the JasperReport object for the filling process.

In order to use JavaScript, the report language attribute is declared below:

language="javascript"

In the report template are presented some JavaScript expressions which cannot be evaluated using Java, and one could notice some advantages that JavaScript scripting comes with.
Having two numbers, 3 and 5, the report will output first their values as real numbers, and then their calculated sum. The two numbers are declared as follows:

  <parameter name="A" class="java.lang.Double">
    <defaultValueExpression><![CDATA[3]]></defaultValueExpression>
  </parameter>
  <parameter name="B" class="java.lang.Double">
    <defaultValueExpression><![CDATA[5]]></defaultValueExpression>
  </parameter>


Both A and B values are declared of java.lang.Double type. But their values are let as primitive int types, because JavaScript is able to allocate types at runtime. All type conversions are performed dynamically, according to the type declarations above. The JavaScript scripting above uses a very simplified syntax. Taking into account the backward compatibility with JDK 1.4.x or earlier, equivalent Java expressions would be:

    <defaultValueExpression><![CDATA[Double.valueOf(3.0)]]></defaultValueExpression>
    <defaultValueExpression><![CDATA[Double.valueOf(5.0)]]></defaultValueExpression>

The next two expressions in the report template read values from parameters declared above and store them in two text fields. These expressions can be evaluated using either JavaScript or Java:

  <textFieldExpression class="java.lang.Double"><![CDATA[$P{A}]]></textFieldExpression>
  <textFieldExpression class="java.lang.Double"><![CDATA[$P{B}]]></textFieldExpression>

Next, the A + B sum is calculated within a JavaScript expression:

  <textFieldExpression class="java.lang.Double"><![CDATA[$P{A} + $P{B}]]></textFieldExpression>

Object instantiation and type conversion are transparent processes here, the user only has to write a simple addition operation between the two report parameters (however, the specific parameter syntax still has to be respected).
The equivalent Java expression would be:

  <textFieldExpression class="java.lang.Double"><![CDATA[new Double($P{A}.doubleValue() + $P{B}.doubleValue())]]></textFieldExpression>

with a lot more complicated syntax.

In order to figure out more on scripting with JavaScript, just test this sample by running from the command line the ant test view command. It will generate all supported document types containing the sample report in the /build/reports directory, and then the report will be loaded and visualized into the built-in viewer.



© 2001-2010 Jaspersoft Corporation www.jaspersoft.com