Jump to content
Changes to the Jaspersoft community edition download ×

  • morlandin
    • Features: Parameters Version: v8, v8.0, v8.0.0, v8.0.1, v8.0.2, v8.0.3, v8.0.4, v8.1, v8.1.0, v8.1.1, v8.2, v8.2.0, v7, v7.9, v7.9.1, v7.9.0, v7.8, v7.8.1, v7.8.0, v7.5, v7.5.2, v7.5.1, v7.5.0, v7.3, v7.3.0, v7.2, v7.2.2, v7.2.1, v7.2.0, v7.1, v7.1.3, v7.1.1, v7.1.0, v6, v6.18, v6.18.1, v6.18.0, v6.17, v6.17.0, v6.16, v6.16.0, v6.15, v6.15.0, v6.14, v6.14.0, v6 .13, v 6.13.0, v6.12, v6.12.2, v6.11, v6.11.0, v6.10, v6.10.0, v6.9, v6.9.0, v6.7, v6.7.0, v6.6, v6.6.0, v6.5, v6.5.1, v6.5.0, v6.4, v6.4.3, v6.4.2, v6.4.0, v6.3, v6.3.9, v6.3.1, v6.3.0, v6.2, v6.2.2, v6.2.1, v6.2.0, v6.1, v6.1.1, v6.1.0, v6.0, v6.0.0, v6.0.1, v6.0.2, v6.0.3, v6.0.4, v5, v5.6, v5.6.2, v5.6.1, v5.6.0, v5.5, v5.5.2, v5.5.1, v5.5.0, v5.4, v5.4.1, v5.3, v5.3.0, v5.2, v5.2.0, v5.1, v5.1.2, v5.1.0, v5.0, v5.0.4, v5.0.3, v5.0.2, v5.0.1, v5.0.0, v4, v4.8, v4.8.0, v4.7, v4.7.1, v4.7.0, v4.6, v4.6.0, v4.5, v4.5.1, v4.5.0, v4.2, v4.2.1, v4.1, v4.1.3, v4.1.2, v4.1.1, v4.0, v4.0.2, v4.0.1, v3, v3.7, v3.7.6, v3.7.5, v3.7.4, v3.7.3, v3.7.2, v3.7.1, v3.7.0, v3.7.0.1, v3.6, v3.6.2, v3.6.1, v3.6.0, v3.5, v3.5.3, v3.5.2, v3.5.1, v3.5.0, v3.2, v3.2.0, v3.1, v3.1.4, v3.1.3, v3.1.1, v3.0, v3.0.0 Product: Jaspersoft® Studio

    Parameters in Jaspersoft are an important aspect of how report templates are built and they can greatly improve the user experience when it comes to generating the right report for the right user and with the right information. It is a concept of the JasperReports Library so it sits right in the heart of the reporting engine within all of the Jaspersoft solutions powered by JasperReports.
    Some Jaspersoft users know parameters as the object required to capture the input control values entered during the report execution so that it can be injected in the query and used as a filter accordingly.

    Report parameters can indeed be used that way and be injected in datasource query but it can do much more than that.

    They are a way to:

    • personalize the information presented in the report output (i.e. add a custom value to be used as a title or to display the server logged-in user name in the report),
    • customize how the information is presented in the report (i.e. a boolean parameter will allow a user to define whether a section of the report should be displayed or not),
    • provide data to be used as a source of the report (it is possible for the JasperReports library to either use the result set generated by a query as well as a full JSON object passed through a parameter to the report as a data source),
    • enforce data governance in the report by filtering the result set of the query specifically for the logged-in user at report execution (when deployed into JasperReports Server).

    However you use parameters, they represent the best communication channel between the report engine and the execution environment (which is your application).

    What is a parameter? 

    A parameter is defined by a ‘name’ and a ‘class’, which is a Java class type like java.lang.String or java.lang.Integer. Any Java class is valid for the parameter ‘class’. For example a parameter of type java.sql.Connection may be used to define a subreport data connection, while a java.lang.Boolean parameter may be used to show or hide an element, a group of elements of the entire section of a report.

    A parameter can have a default value which is defined by means of the default expression property. This expression is evaluated by JasperReports solely when a value for the parameter has not been provided by the user at run time. For example, the default value may be used during reports preview in Jaspersoft Studio.

    How to create and manage parameters?

    To create a new parameter, use the outline view.

    • add a parameter by right-clicking on the item "Parameters" in the Outline panel,
    • choose "Create Parameter".
    • in the property panel, configure:
      • Its name;
      • Its class;
      • Whether it should be used for the prompt or not (which will require an input control at run time if true);
      • Optionally add a description, a default value;
      • Since version 7 it is also possible to set the parameter evaluation time to late or early which will define whether the parameter default value should respectively be calculated before or after running the report data query. 

    To delete a parameter from the outline view right click on it and select "Delete". The parameters with a light gray name are created by the system and can not be deleted or edited.

    1(18).png.69bb2038161e7aa1f12df6f8194957d0.png

    Built-in Parameters

    All reports contain a set of built-in parameters, parameters available by default that contains some key run time information. Some of the most important ones are:

    • REPORT_CONNECTION, which holds the JDBC connection used to run the SQL query of the report (if the report is filled using a JDBC connection)
    • REPORT_DATA_SOURCE which contains, if available, the data source used to fill the report
    • REPORT_LOCALE which contains the Locale used to fill the report

    Some built-in parameters are specific to a specific query language, for example when using the XML query language, the report will automatically include the parameter "XML_INPUT_STREAM" which defines the records to be used in filling the report. The built-in parameters can not be modified or deleted.

    Using Parameters in an SQL Query

    Parameters can be used in SQL queries to filter records in a where condition or to add/replace pieces of raw SQL or even to pass the entire SQL string to execute.
    In the first case, the parameters are used as standard SQL parameters, in this example we refer to the parameter value with the syntax $P{...}:

    SELECT * FROM ORDERS WHERE ORDER_ID = $P{my_order_id}

    In this example, my_order_id is a parameter that contains the ID of the order to read. This parameter can be passed to the report from the application that is running it or by the user running the report to select a specific order. Please note that the parameter value here is directly used as a SQL parameter. This means that the query will be executed using a prepared statement like:

    SELECT * FROM ORDERS WHERE ORDER_ID = ?

    and the value of the parameter my_order_id will then be passed to the statement. 

    Now let's consider this second case which refers to a parameter using the $!P{...} syntax (note the extra exclamation mark):

    SELECT * FROM ORDERS ORDER BY $P!{my_order_field}

    In this case, my_order_field is not directly used as a SQL parameter. JasperReports will consider this parameter like a kind of placeholder that will be replaced with the text value of the parameter (which may even be a portion of the SQL statement such as "ORDERDATE DESC").
    With the same logic, a query can be fully passed using a parameter. The query string would then look like

    $P!{my_query}

    The number of parameters in a query is arbitrary. When passing a value using the $P!{} syntax, the value of the parameter is taken as is, and the user is responsible for the correctness of the passed value (SQL escaping is not performed by JasperReports in this case). 

    When using a parameter in a query, Jaspersoft Studio requires that a default value would be set for the parameters used in the query to that it can retrieve the fields automatically.

    Note: You should be extremely careful when using a parameter in a query through the $P!{} syntax. If the parameter is also used for prompt then that would allow the user to inject a query and thus potentially access some unauthorised data. It is then recommended to have one or more parameters requesting specific information through input controls and then use these values in the parameter default value which will generate the appropriate portion of SQL query that you’d like to inject in the query.

    Parameters Prompt

    If a parameter is set to be used as a prompt, then when executed the report will require to enter a value for these parameters. This is true with Jaspersoft Studio,  JasperReports Server, or any other solutions which use the JasperReports Library.

    Here is a simple example.
    > Create a simple report with the template "Blank A4", name it "ParameterExample"
    > Use the data adapter "One Empty Record - Empty Rows".
    > In this report, create a parameter, and using the Properties panel,

    • name it MESSAGE,
    • set its type to String,
    • and set it to be used as a prompt (the property "Is For Prompting" must be checked).

    > Drag the parameter from the outline view into the title band which will trigger Jaspersoft Studio to create a text field displaying the parameter value.
    > You should have something like the following image:

    2(16).png.26eba00e3d7ab062f14addc65e892cfa.png

    Now, when executing the report, using the Preview button, for example, the parameter prompt dialog appears asking for a value for the MESSAGE parameter

    3(10).png.5fe06fd6a545441196dbe6ebcdd667a6.png

    Set a value for the parameter (i.e. Hello World!) and hit the "Play" button. The message will be printed in the title band

    4(11).png.019004c7c6776f825b5e5a9f51af2d4d.png

    Jaspersoft Studio provides input dialogs for parameters of type String, Date, Time, Number, and Collection.

    • Haha 1

    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...