Skip to main content

JSF + JPA + JasperReports (iReport) Part 2

In this post  is  a continuation of Jasper Report Part 1. here we will discuss about some advanced jasper report concepts like passing complex object like List, Date object and how to create and use local variable in jasper report, Add the sub-report and background image to report. Check my latest post about Integrate Charts into Jasper Reports.

here i am using JPA (Java Persistence API) for accessing the Database. so i create the two entity ShoppingCart and Item. here ShoppingCart entity contain list of Item entities. then i am going to pass these objects to Jasper Report. (Check the video in below)

ShoppingCart.java

Item.java

Create Report
          now i am going create report. we can use iReport standalone or iReport Netbeans Plugin to create the report. first we need to create some fields in report. these fields are matched with ShoppingCart Bean property names and Data types also.


and set the data types of these fields correctly.

firstName  ==> java.lang.String
lastName  ==> java.lang.String
dop  ==> java.util.Date
listOfItems ==> java.util.List

then drag these fields to detail pane of the report as per needs. then right click the Report and compile the report



then its creates the jasper file. and its compiled format of jasper report. check part 1 for more details of overview of jasper report.

now create one JSF page with single Command Button (Submit Button)
and we create one managed bean for above JSF page
DemoBean.java

Now when u run the project its generate the report in PDF file format.

now we integrate the sub report. so drag the sub-report component from palettes. then add the Item beans property as a fields names and correct data types.

and data types
itemName ==> java.lang.String
price ==> java.lang.Float
noOfUnits ==>  java.lang.Integer

once sub report is ready then compile the subreports.

while create the subreport we passed the Empty Data Source. so now we need to change to listOfItems as a data source, we can't pass directly because its not accept List. so we need to wrap into the JRBeanCollectionDataSource Object.

so in main report select the sub report and change the properties

Connection Type ==> Use a datasource Expression
DataSource Expression ==> new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{listOfItems})




You Can Download this complete example from GitHub (or) google code .

How to use

  1. Change the properties in persistence.xml file, like db name, db hostname ,username and password.
  2. Add necessary lib to class path like Hibernate libs, MySQL lib, Jasper Report lib. for simplicity in this zip contain war file under dist/ folder. so just extract   and see all lib are located in WEB-INF/lib section. copy all files and add to to ur class path (Libraries )
  3. Create the jasperdb database in ur mysql server
  4. First run the NewServlet , this servlet create necessary tables in ur database. and insert some sample rows to the table.
  5. Finally run the index.xhtml file and generate the report.


I deployed same example in Jelastic Paas cloud, you can access through http://ramkicse.jelastic.servint.net/jasper/ URL
click the button to generate the report. (Its expired)

check How to deploy the Web application to Jelastic cloud


Check my latest post about Integrate Charts into Jasper Reports

Screen Cast (Watch the video in HD)

Comments are Welcomed

Popular posts from this blog

Understanding Virtual Host Concept in Tomcat

Hi in this post we will see how to setup virtual host in Apache Tomcat server. Virtual Host is in-built feature that allows to deploy multiple website(domains) in single instance of tomcat server. The main benefit in this way is its cost effective. Scenario: I am going to deploy 3 website with following domain names in single tomcat http://www.ramki.com http://www.krishnan.com http://www.blog.ramki.com The following diagram is my outline. Outline structure of Virtual Host Concept in Tomcat Here my tomcat IP address 192.168.1.15. or any IP address allocated my ISP. but it should be public IP address. How all domain names are pointing to my Tomcat?                   When we purchase the domain name we need to update the our tomcat IP address to it. like or we can simulate same DNS Setup through hosts file in both Linux and Windows. In Linux tha file is located at /etc/hosts Now How Setup...

Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)

In this post i will show Tomcat Clustering in Docker Container. In  my previous post i discussed how to achieve tomcat clustering with Nginx Front end . Its almost same scenario, but this time we will achieve via docker container. Docker Docker  is an  open-source  project that automates the deployment of  applications  inside  software containers , by providing an additional layer of abstraction and automation of  operating-system-level virtualization  on  Linux . [4]  Docker uses resource isolation features of the Linux kernel  such as  cgroups  and kernel  namespaces  to allow independent "containers" to run within a single Linux instance, avoiding the overhead of starting and maintaining  virtual machine   --Wikipedia

Virtual Host + Apache httpd server + Tomcat + mod_jk connector

In my last post ( Virtual Host in Tomcat ) we discussed about how setup the virtual host in Tomcat. Its cost effective technique because only one public IP is enough to host multiple domain. If we have big organization and each department want to host their website in locally in different machine. then how to achieve the virtual host concept?. In this post we will see the how we do this. Update :   I posted  Virtual Host + Nginx + Tomcat  Its easy to configure, compare to Apache httpd server Problem Scenario:         In big organization they have multiple department, each department want to host their website in different machine. so these websites are accessed locally with different local IP address. When we mapping to public address then we face the problem. We have two choice either purchase as many public address or Put one server front  and delegate these request. ...