Saturday, May 22, 2010

Jasper Report with Spring

Jasper Report with Spring

Spring supports different views as JSP, HTML , Jasper and custom view. Here I trying to provide some technique and ideas how to integrate Jasper report view in spring. Jasper is a third part y tools to design the data representation in view in different format. Jasper supports different type of format for the view.

Spring is provided some classes to integrate jasper report with the spring. Spring provides some classes in org.springframework.web.servlet.view.jasperreports this package for jasper. To integrate the jasper with spring application follow the steps given bellow:-

1. Place jasperreports-3.0.0.jar, spring.jar, poi-2.5.1.jar, commons-collections-3.2.jar, commons-beanutils.jar, commons-pool.jar and commons-io-1.3.2.jar jar files or its’ higher version of those files in the class path.

2. Provide bean for report view in [servletName]-servlet.xml file

<bean id="viewReportResolver"

class="org.springframework.web.servlet.view.ResourceBundleViewResolver">

<property name="basename" value="ReportsConfig"/>

bean>

3. Create ReportConfig.proerties file for the view mapping property and place it in the /WEB-INF/classes directory.

4. Provide report view mapping in the ReportConfig.proerties file

# Jasper View class name

viewName.class=org.springframework.web.servlet.view

.jasperreports.JasperReportsMultiFormatView

# Jasper report name

viewName.url=/JasperReport.jasper

# datasource key (pass data list as datasource.

#Pass [dataList] as example of given bellow controller code)

viewName.reportDataKey=datasource

# this configuration for remove image assci charecter

viewName.exporterParameters[net.sf.jasperreports.engine.export

.JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN]=false

5. Your controller method information will be

ModelAndView modelAndView = new ModelAndView("viewName ");

modelAndView.addObject(“format”,”pdf”);

modelAndView.addObject("datasource ", [dataList]);

Example:

Project name: Spring Report

web.xm

xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version="2.4">

<display-name> Spring Report display-name>

<description> Spring Report description>

<servlet>

<servlet-name> springreport servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

servlet-class>

<load-on-startup>2load-on-startup>

servlet>

<servlet-mapping>

<servlet-name> springreport servlet-name>

<url-pattern>*.htmurl-pattern>

servlet-mapping>

beans>

Springreport.xml

xml version="1.0" encoding="UTF-8"?>

beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean id="viewReportResolver"

class="org.springframework.web.servlet.view.ResourceBundleViewResolver">

<property name="basename" value="ReportsConfig"/>

bean>

<bean id="reportMAC"

class="spring.report.mac.ReportMultiActionController">

bean>

<bean id="methodNameResolverRef"

class="org.springframework.web.servlet.mvc.multiaction

.PropertiesMethodNameResolver">

<property name="mappings">

<props>

<prop key="/jasperReport.htm">reportMethodprop>

props>

property>

bean>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<props>

<prop key="/jasperReport.htm"> reportMAC prop>

props>

property>

bean>

beans>

ReportConfig.properties

viewName.class=org.springframework.web.servlet.view.jasperreports

.JasperReportsMultiFormatView

viewName.url=/JasperReport.jasper

viewName.reportDataKey=datasource

viewName.exporterParameters[net.sf.jasperreports.engine.export

.JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN]=false

Controller Class

package spring.report.mac;

public class ReportMultiActionController extends MultiActionController {

public ModelAndView reportMethod (HttpServletRequest request,

HttpServletResponse response) throws ServletException {

List stringList=new ArrayList();

//write data access code here and place all data in the list

ModelAndView modelAndView = new ModelAndView("viewName ");

modelAndView.addObject(“format”,”pdf”);

modelAndView.addObject("datasource ", stringList);

return modelAndView;

}

}

JasperReport.jasper

Design jasper report in iReport for your data presentation.

Run run application for report:

Deployed your application in the server and send the request URL as http://localhost:8080/springreport./jasperReport.htm

Note:

Spring supports Jasper report format only PDF, XLS, CSV and HTML. If you want to print another format you have to override the JasperReportsMultiFormatView class. I will discussed in my next article.

No comments:

Post a Comment