JasperReports Ultimate Guide - Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)
|
|
|
|
JasperReports - JavaScript Sample (version 4.6.0) | ![]() |
|
|
|
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 |
|
|
||||||
|
|||||||||
|
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">
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. Running the Sample Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/javascript within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/javascript/build/reports directory.
Then the report will open in the JasperReports internal viewer. |
||||||||
|
|
© 2001- Jaspersoft Corporation www.jaspersoft.com |