Interview Questions

Spring Jasper Reports Integration

What is Jasper Reports ?
Jasper Reports is an open source report engine to generate reports in various formats like pdf, html, xls and csv etc,. To design the layout and format of the report to be generated, we need a tool called iReport. Using iReport, we can generate the template for the report to be generated. You need to specify a report query (sql query) and map the output columns of the sql query to the report template. The template will be in (*.jrxml) format. This template (xml) format need to be compiled in to (*.japer) format. This can be done programmatically or using iReport tool itself. The report query can also be passed at run time before compiling the report, but the columns from the query should match the fields in the report template.

Now let's see how to generate reports using jasper in a spring based application. Spring provides easy to use api's for integrating jasper reports. First create a spring config file as follows


<bean id="jasperReportsController" class="in.techdive.spring.JasperReportsController">
    <property name="methodNameResolver">
        <bean id="jasperReportsControllerMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
            <property name="mappings">
                    <prop key="/addeir/reportView.htm">reportHandler</prop>

    <property name = "jMultiView" ref = "jMultiView"/>

<bean id="jMultiView" class="org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView">
    <property name="jdbcDataSource" ref="dataSource"></property>

    <!-- Either a (.jrxml or .jasper) file path can be mentioned in the url
    property. If it is a (.jrxml) file the api will compile it into (.jasper)
    <property name="url" value="/Techdive_REPORT.jrxml"></property>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="url" value="<your_url>"/>
   <property name="driverClassName" value="<your_db_driver_name>"/>
   <property name="username" value="<db_user_name>"/>
   <property name="password" value="<db_pass_word>"/>

Consider the bean JasperReportsMultiFormView, for which we need to assign two properties mainly jdbcDatSource and url. The jdbcDataSource property is used to connect to database at runtime and retrieve the results for the report query. Either a (.jrxml or .jasper) file path can be mentioned in the url property. If it is a (.jrxml) file the api will compile it into (.jasper) file.

Now let's create the class as follows.

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView;

public class JasperReportsController extends MultiActionController
        private JasperReportsMultiFormatView    jMultiView;

        public JasperReportsMultiFormatView getjMultiView()
                return jMultiView;

        public void setjMultiView(JasperReportsMultiFormatView jMultiView)
                this.jMultiView = jMultiView;

        public JasperReportsController()


        public ModelAndView reportHandler(HttpServletRequest request, HttpServletResponse response) throws Exception

                ModelAndView mv = null;
                        Map<String, Object> model = new HashMap<String, Object>();

                        String reportType = (String) request.getParameter("reportType");
                        if (reportType == null)
                                reportType = "pdf";

                        System.out.println("reportType --> " + reportType);

                        model.put("reportType", reportType);
                        // by default the formatKey will be "format".

                        mv = new ModelAndView(jMultiView, model);

                catch (Exception e)
                        System.out.println("Exception Occured  profileHandler-> " + e);
                return mv;

Use the url http://localhost:8080/reportApp/reportView.htm?reportType=pdf to generate report. You need to pass reportType as parameter to generate report of required format.
In the above class, We are injecting the JasperReportsMultiFormatView property to the above JasperReportsController class. Consider the reportHandler method which will be called when the above url request is made in the browser. In this method we are setting a FormatKey property of JasperReportsMultiFormatView to specify the report format type as (pdf, xls, csv or html). This will be used for rendering the jasper report by the jasper engine internally.

In this way spring framework provides easy to use API's for integrating jasper reports.