Working with JasperReports for easy report generation

转自:http://www.jcombat.com/spring/working-with-jasperreports-for-easy-report-generation

JasperReports is a Java based open source report generating engine. It has great dynamic report generating capabilities that can be added to any Java based application. I have a very simple demo to show how it works.

I have created a link somewhere in my application and on the click of it, I see a pop-up window with the report PDF embedded within.

To start with, we need a .jrxml template that is externalized and every time the link is clicked, the.jrxml template is dynamically compiled to generate a .jasper file at the same external file location. So the first and foremost step is to prepare the mark-up for the report template, which can be easily designed using the iReport tool. The tool finally generates the .jrxml file based on the design. The standard JasperReport template has the following layout in the same order:

Fine, now let’s assume, I have my .jrxml file generated and looks something like:

I keep the above .jrxml file to some externalised location, let it be, D:/Documents/


Create a sample POJO, with two instance variables for subject name and marks, as:

DataMarksBean.java

Create another class DataBeanMarksList.java, which generates a collection of java bean objects to be used for report generation. This class looks something like:

DataBeanMarksList.java

Now you need to create a method in the controller of your Spring MVC application, that actually serves the purpose of report generation. I give it some mapping as /jasper

Almost done, create a link somewhere on the landing page in your application as:

Male sure you include the following javascript function as well into your application:

Finally, clicking on the link, shows up the report PDF in a new pop-up window as:

Jasper Reports

If you need the same report to be exported to HTML, just modify the above controller method and add the following:

Add a new method renderHtml as:

So we have several points worth noting:

  1. The .jasper file is not kept anywhere in the JVM for any subsequent PDF generation and for now, is generated every time the link is clicked.
  2. As expected, if we modify the .jrxml template, it reflects instantly at runtime and we do not require any java changes, hence no build/deploy needed.
  3. As per the documentation available for JasperReports,  “Even though all its reporting functionality is available in this single JAR file (jasperreports-5.0.1.jar), JasperReports relies on other third-party libraries for related required functionality like XML parsing, logging, and PDF generation.”

To avoid performance hit on the application because of the .jrxml file being compiled again and again, I suggest following options to restrict the subsequent compilations:

  1. We can keep the .jasper file in cache and generate the .jasper soon after the cache expires.
  2. We can also check for the existing .jasper file at the respective externalized location. If it doesn’t exist, compile, else not.
  3. We can build a utility to explicitly compile the .jrxml template, generate the .jasper file and copy it to the respective externalized location.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值