Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between variable, parameter and field in JasperReports?

I am a newbie to JasperReports, have been working on some small samples. It seems "Fields", "Parameters" and "Variables" are very commonly used to demonstrate dynamic data and looks much alike. So can I ask what's their difference specifically in JasperReports?

I guess variable is something defined within a Jasper report and can dynamically change. Parameter is something taking from external source (Java..etc), field is for entities (database schema, class entity), but I don't think my understand is all right.

like image 426
Dreamer Avatar asked Jun 14 '12 20:06

Dreamer


People also ask

What is the difference between a parameter and a field?

Usually a parameter will hold an object with a value, like a String or a primitive type. Fields might hold a collection of beans, objects etc...

What is parameter in Jasper?

A parameter can have a default value which is defined by means of the default expression property. This expression is evaluated by JasperReports only when a value for the parameter has not been provided by the user at run time. To manage the parameters, use the report inspector.

How do you use variables in JasperReports?

From there you can create a new variable (right click on the "Variables" item and then "Create variable") and, once selected, a variable its properties are visible on the Properties tab. There are some built-in variables on Jaspersoft Stuido, which present in every report.

How do I set parameter value in Jasper Report?

To add a parameter to a Jasper Report using Jaspersoft Studio it is an easy task. Into the Jaspersoft Studio right click on "Parameters" and choose "Create Parameter". Now you can use that report using a parameter. This parameter will be passed when the report will be called from Java for instance.


2 Answers

Parameters are simple input to JasperReports. You have to define parameters in the JasperReport before using them. You can display the value of the variable, you can use it as part of boolean expressions and you can even pass it to subreports. This can be an input to SQL query.

Fields are simple variable definitions. You can think of these as instance variables of the datasource object thats passed in to the report or they can be key names if the datasource is a Map. If you configure JasperReport to create the dataset based on SQL, then Fields are the column names of the ResultSet. You will use Fields to display the resultset of an executed SQL query.

Variables are another kind of variables that live within Jasper Report, they are not inputs. They are used to calculate sum or average of certain Field (defined above). You can perform many other predefined calculation functions on the Fields using Variables.

like image 122
sperumal Avatar answered Sep 22 '22 08:09

sperumal


From my personal experience with JasperReports i can deduce that you will be using Parameters and Fields the most. Parameters and fields are memory locations or values which you can populate from your code, i.e when you generate the report.

What you would usually be doing is populating a parameter map or maps with different settings for your report. I use parameters if i have a summary page or a cover page (the very first in a report) Something like:

parameters.put("authorName", author); //where authorName is a parameter you have created in your JRXML template. 

Next, you might be using some custom "variables" or you might be using variables provided from JasperReports. Some of those useful variables are: PAGE_COUNT and PAGE_NUMBER. They keep track of... report page counts and page numbers. Of course you can have custom variables.

Fields are used where data changes frequently. They are quite similar to parameters but with each iteration the data might change. Like, a field might be a list of germanCar objects for one iteration and a list of japaneseCar object for the next. I would use a field to hold the list of Car objects that might change.

Bottom line is parameters and fields are quite similar, but fields are populated from the JasperReportDataSource (so they can change frequently as you are populating that datasource), while parameters you would use for cover pages or custom JR settings WHILE generating the report itself. They could be quite confusing.

Hope this helps a bit!

like image 30
Mechkov Avatar answered Sep 25 '22 08:09

Mechkov