Backend Development

Making Dynamic Tables with Jasper Report and Spring Boot

Jan 6, 2023

  • 8 mins read

Jasper report is an open-source java class library that aids developers to add reporting capabilities to their Java applications.

Sadia Zahin Prodhan
Sadia Zahin Prodhan

Making Dynamic Tables with Jasper Report and Spring Boot

Jasper report is an open-source java class library that aids developers to add reporting capabilities to their Java applications. The most common way to represent data is in a tabular form, but this is perhaps a bit tricky in jasper report. Worry not because this post tells you all about making dynamic tables with Jasper Report. Without any further ado, let’s get started.

Let’s say you work for a company and they want you to generate a report that displays all the employees' information in the table and at the bottom of the table, you have to display the number of employees the company has.

To do the above task in Spring Boot, you need to have an employee table. Let’s assume you just need to display the id, name, and department of each employee. Hence your Employee class should look something like this:

Making dynamic tables with jasper report

You have to write your necessary Repository, Service, and Controller code, I am not going into much detail about that, since that is not our main goal here. Okay, we have the necessary model ready now, it’s time to dig into our report.

Getting Started with Jasper Report Table

You can use the Eclipse plug-in, or you can download Jaspersoft studio from JasperSoft Community -https://community.jaspersoft.com/project/jaspersoft-studio/releases.

After setting up your JasperSoft studio, you can begin by creating a new Blank A4 jasper report. You have to make sure that you choose Data Adapter- One Empty Record - Empty rows in the Data Source section.

You will see your blank report has been created. Now did you notice a section called Palette at the right-hand side of JasperSoft studio. If you scroll down in the basic element section in Palette you will see an option named Table.

Making dynamic tables with jasper report

Click on it and draw a table in the detail 1 section of your main report. As soon as you finish drawing you will see a window pop up. Now, the next steps are really important if you want to pass your dataset from your database to your jasper report in Spring boot. In the pop-up table window, first, you have to select a dataset. Choose "Create a Table with a new dataset" and click "Next".

Making dynamic tables with jasper report

Secondly, choose "create an empty data set" and give your data set a name, and click "Next".

Making dynamic tables with jasper report

In the connection section, choose “Use an empty Data Source” and click Next.

Making dynamic tables with jasper report

After that just click “Next” and choose your layout and you will be ready to go. In the main report, if you double click on the table it will take you to the Table section and there you can edit your table. Right-click on a table column gives you a variety of options. You can create a column before or after the selected column.

Since we have three fields in our employee class, we will need three columns. You can resize every column by selecting the column first and dragging the sides with the specified arrows.

An outline in the bottom left-hand side of the table gives all the column information and the empty dataset that you have created.

You can see a table consisting of 5 sections.

  1. Table header - If your Table has a title you can put it here.
  2. Column Header - Contains the title of each column of the table.
  3. Detail - This is the main part of the table. You have to put all your table fields here. This part is dynamic and all your information from the dataset will be shown here.
  4. Column Footer - If you need to put anything at the end of your dataset you can put it here.
  5. Table Footer - Same as column footer.

Okay if you understand till now, you are good to move further.

In the column Header in each cell write your necessary column title with static text. You can format the font size, font style and alignment of each static text. Now go to your main report and the table would look something like this.

Making dynamic tables with jasper report

Configure Your Dataset

The next step is very very crucial, you have to do everything right to make things work accurately. In the main report, at the bottom left-hand side you will see Parameters, right-click on it and choose to create a parameter

Making dynamic tables with jasper report

Give your parameter a name, such as “CollectionBeanParam” and choose your class as “JRBeanCollectionDataSource”. You can find this option in the three dot box beside class name.

Making dynamic tables with jasper report

Now it’s time to create your table fields.

Go to your table’s outline and under your dataset name- “MyDataSet” create fields.

Now be careful while naming your fields. You have to make sure you give the same name as your class’s attributes. In this case, you need three fields: id, name, and department. Make sure you choose the correct classes for each field. Id should be java.lang.Long and others should be java.lang.String. Now drag and drop the fields to their corresponding detail cell.

Making dynamic tables with jasper report

The next step is to set your dataset.

From the outline right click on the table and select show properties. In the properties section go to the dataset tab and select “ Use a JRDataSource expression”. You can see an edit button beside it. Click on it and a window will appear.

In the expression editor, click on parameters and then choose the CollectionBeanParam that you have created a while ago. Lastly click “Finish”.

Making dynamic tables with jasper report

Table Parameters

Now we have another small task left, at the beginning of this post I said you have to show the number of total employees in the table. Since it is not a part of our dataset but the value will come from our backend service we can pass it in the parameter. This is a bit tricky since we want to display this in the table. At first we need to declare a parameter in the table dataset which is the MyDataSet.

So let’s create a parameter called tableNoOfEmployees and a static text that says “ No of employees”

I have deleted all the column footer cells, since we don’t need it. This is what it should look like.

Making dynamic tables with jasper report

Now, go to the main report and create another parameter called “noOfEmployees”. We have to do this because you cannot pass values directly from your spring boot service or controller to your table parameters. You have to pass the values to your global parameters which you have to map with your table parameter. Ok, after you have created your global parameter “noOfEmployees”, your job is now to map this parameter to your table parameter. In order to do so go to your table properties, dataset section. At the bottom you can see parameters, click on it. A window will appear, now your job is to map the parameters. Click on add, select the table parameter and for the expression, you have to select the global parameters. See the photo below if you have any confusion.

Making dynamic tables with jasper report

Your work with JasperSoft studio is done, if you are using a plug-in the jrxml file is already generated, and if you are using the studio you have to copy the source file and paste it in a new created jrxml file in your resource folder of your spring boot application.

You have to determine the classpath of your jrxml file to your InputStream and compile a jasper report with it. After that you have to create a datasource with your dataset, for example, here our dataset is the list of employees.

Now to the important part here is you have to put the parameters name exactly how you have declared in your jasper report. The CollectionBeanParam will map your dataset to the fields in your jasper report.

The no Of Employee parameter will take the value and pass the value to your mapped table parameter. If you want to download it as a pdf you have to export it.

Making dynamic tables with jasper report

That’s it for today. Thank you for reading. See you around. If you have any questions, you can email at info@dhrubokinfotech.com.

Next


 

Suggested Reading

Spring Boot JPA Performance Tuning: Speeding Up Hibernate Batch Processing Time

Backend Development

Spring Boot JPA Performance Tuning: Speeding Up Hibernate Batch Processing Time

Nov 2, 2023

  • 2 mins read

In this blog post, one of our developers discusses one of the hibernate performance issues we faced during one of our projects and how we resolved it.

Hafizur Rahman
Hafizur Rahman

Handling Circular References in Java Spring Boot

Backend Development

Handling Circular References in Java Spring Boot

Nov 4, 2023

  • 7 mins read

Here one of our developers shares his experience with handling circular references in Java Spring Boot.

Hafizur Rahman
Hafizur Rahman

Kotlin vs. Java: The Battle of Languages in Android Development

Backend Development

Kotlin vs. Java: The Battle of Languages in Android Development

Dec 20, 2023

  • 2 mins read

The mobile application for Android is growing rapidly. As a result, the programming languages Kotlin and Java have been fiercely competing.

Guest Author
Guest Author

Contributor

logo

Dhrubok Infotech Services Ltd. (DISL) is one of the top full-service software development companies in Dhaka, Bangladesh that delivers up-to-the-minute iOS, Android Apps and Enterprise Web Solutions. We exist to help startups and enterprises of all sizes to build better products, reach more people and have a prominent online presence.

Follow Us

© 2024 Dhrubok Infotech Services ltd.

Terms of Use & Privacy Policy