Tuesday, January 31, 2012

Spring MVC 3.1 and JasperReports: Using iReport and AJAX (Part 1)

In this tutorial, we will study how to generate reports from a Spring MVC 3.1 application using JasperReports. We will use jQuery and jqGrid to provide AJAX support and an interactive table. To design the report, we will use iReport, an open source report designer for JasperReports. On the data layer, we'll utilize MySQL, Hibernate, and Spring Data JPA.


  • Spring core 3.1.0.RELEASE
  • Spring Data JPA 1.1.0.M1
  • jQuery 1.6.4
  • jqGrid 4.3.1
  • Jasper Reports 4.5.0
  • See pom.xml for details


To access the source code, please visit the project's Github repository (click here)

Functional Specs

Before we start, let's define our application's specs as follows:
  • Display reports in an interactive table
  • Downloadable reports in PDF and Excel formats
  • Use AJAX to avoid page refresh
  • Users have roles. They are either admin or regular (default)
  • A username is assumed to be unique

Here's our Use Case diagram:
[User]-(Download Pdf) 
[User]-(Download Excel)


Our database contains two tables: user and role tables.

user and role table design

User table

The user table contains personal information of each user. Notice the password values are hashed using Md5.

user table

Role table

The role table contains role values of each user. We define a role value of 1 as an admin, while a role value of 2 as a regular user.

role table


Before we start with the actual development, let's preview how our application will look like. This is also a good way to clarify further the application's specs.

Entry page
The entry page is the primary page that users will see. It contains an interactive table where users can view and download records.
Entry page

Pdf document
This is the downloadable Pdf document whenever the user clicks on the Pdf button.
Report in Pdf format as previewed from Chrome

Excel document
This is the downloadable Excel document whenever the user clicks on the Excel button. When using Excel format, the document's appearance varies depending on the application, i.e Microsoft Excel, Google Docs, Open Office.
Report in Excel format as previewed from Google Docs

Progress dialog
When a user clicks on any of the download buttons (Pdf or Excel), a progress dialog will appear and disappears once the download starts.
Progress alert


In the next section, we will discuss the project's structure and write the Java classes. Click here to proceed.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring MVC 3.1 and JasperReports: Using iReport and AJAX (Part 1) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share


  1. Hello Marks
    you are great, I appreciate you work , you explained so well that anyone can understand
    keep it up
    Hats off to you!!!

  2. SAM

    Hi Krams, This is a great great tutorial...thanks for your time and effort. really helpful and very much easy to understand from the beginning. i took ur source code sample and set it up on my local. it was awesome. thanks a lot and appreciate your effort.

    keep up the good work..


  3. Hi Krams,

    I just tried to open that pdf file in a popup window. it works perfectly for google chrome but not for IE. i changed the javascript code as follows.
    // Start download
    //window.location = '${downloadUrl}'+'?token='+token+'&type='+type;
    var url = '${downloadUrl}'+'?token='+token+'&type='+type;

    also i asked this question in stack overflow as well. can you please let me know is there any additional parameters that i need to set from the serverside?

    Thanks in advance.


  4. @Sam, honestly I have not tested this application with IE. I only tested it with Google and Firefox (both in Ubuntu and Windows 7). The best advice I could provide is have a JavaScript that will detect whether your user is using IE or a different browser. I'm sure there are already js libraries there that this for you. Maybe try Modernizr (http://www.modernizr.com/)?

    I don't think the issue is with the server side. If it does, then it must be the HTTP headers. But I'll put my bet that this is more a browser issue.

  5. @Krams - i tried to use your logging technique with 'CustomizableTraceInterceptor' with spring DM OSGI (Eclipse Virgo) server. but it wont print to the log. are there any specific way to doing it with respect to the OSGI ?

    1. samitha, I am sorry but I haven't tried that CustomizableTraceInterceptor with Virgo. I'm not sure if it's supported. Take a look at the following discussion:

      http://www.eclipse.org/forums/index.php/t/303958/ and http://www.eclipse.org/forums/index.php/m/768584/

    2. @Krams - Thanks a lot for the postings..yes it seems some thing wrong with AOP weaving when it comes to OSGI.earlier i had issues with CGLIB and related bundles.i was able to solve those but it seems pointcut is not executing...Thanks a lot your effort krams.....Thanks..

  6. i am not getting source code of this project would you please share the link to download please

  7. Thanks for great tutorials.
    I have downloaded the source code and run it. It throw error due to repository for maven-copy-plugin have been changed. So if you meet the issue with maven-copy-plugin when building app, please update the pom.xml with following content:

    Evgeny Goldin Repository

    Luan Vo

    1. Please see my post about the new issues and fix for this plugin http://krams915.blogspot.com/2012/08/copy-maven-plugin-updates.html

  8. Didn't work for me I am using

    Evgeny Goldin Repository

    1. Please see my post about the new issues and fix for this plugin http://krams915.blogspot.com/2012/08/copy-maven-plugin-updates.html

  9. please change your pom evgeny-goldin plugin, the artifact is changed, i changed to this

    Evgeny Goldin Repository

    1. Please see my post about the new issues and fix for this plugin http://krams915.blogspot.com/2012/08/copy-maven-plugin-updates.html

  10. thank you for your beautiful tutorial..

    i was following you tutorial in my college project to produce report using jasper report..and i am using spring 3 with hibernate and now i need to integrate to jasper report but i was unable to do that..

    in your tutorial you are using maven but i'm already using ant for build my project...
    can you tell me how to do this compiling stuffs of jrxml file with ant and use it with spring...this will be very helpful for me..also ireport has compiling feature on itself..so does .jasper produce my ireport can be used in project and how....

    please share your valuable knowledge

    thank your...

  11. I want to get Word report using jasper report in Spring MVC.can anyone help me.

    1. I think you'll need a library similar to Apache POI.

  12. Hi. Thank you very much for your blog. I have a similar request for a progress bar to be shown while jasper report being generated. Is it possible to show a progress bar with percentage amounts very similar to the one Windows Explorer uses, when copying files across, i.e. the green bar.

    1. This would be probably tricky. Maybe you can write an AJAX service that polls the backend if a download complete token is existing. Check one of my articles with FileUpload. I believe I have a strategy similar to that.

  13. Nice article! This is what I was looking for :)

    Rajesh Gheware

  14. Kindly share these files custom.js,ui.jqgrid-4.3.1.css and style.css ,Because i am not able to download it

  15. Kindly share these files custom.js,ui.jqgrid-4.3.1.css and style.css ,Because i am not able to download it

  16. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

  17. If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you got to watch this video
    right away...

    (VIDEO) Get your ex back with TEXT messages?

  18. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    core java training in Electronic City

    Hibernate Training in electronic city

    spring training in electronic city

    java j2ee training in electronic city

  19. The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Project Centers in Chennai for CSE.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

  20. Thank you for posting informative insights, I think we have got some more information to share with! Do check out Java Training in Chennai and let us know your thoughts. Let’s have great learning!