Building JasperReports Views

JasperReports is an open source reporting engine written in 100-percent Java. JasperReports has been in development for quite some time and provides a comprehensive feature set for building reporting applications using Java. The current version of JasperReports, 0.6.1, provides support for four output formats: CSV, Microsoft Excel, HTML, and PDF. Using Spring's Jasper- Reports view support, you can easily integrate JasperReports into your web applications.

To run the example in this section, you need to download JasperReports from http://jasperreports.sourceforge.net.

Designing Reports

Unlike many commercial reporting tools, JasperReports uses an open, XML-based file format for the report designs. For those of you who have wrestled with graphical report designers in the past and have tried to get the report layout just right, the ability to edit a report design at such a low level will no doubt be more than welcome. Thankfully, however, you do not need to hand-code all your report files because there is a wide range of graphical tools for working with JasperReports design files. You can find complete details of these tools on the JasperReports website.

For the example in this section, we use one of the sample report designs that comes with JasperReports. We are not going to show the code for the report design here, because there is quite a lot of it and this is not a chapter about JasperReports. It is enough to know that the report design displays a list of people's names along with the street and city in which they live. The full code for the report file is available in the code download (visit this chapter's folder in the Downloads section of the Apress website at www.apress.com).

Compiling Reports

Before you can use reports, you must compile the design file, which has a .jrxml extension, into a report file, which has a .jasper extension. When using JasperReports with Spring, you have two options for report compilation: you can use the Ant task that comes with JasperReports or you can let Spring compile your report on the fly. When associating a Spring Controller with a JasperReports view, you can provide either the .jasper or the .jrxml file. When you supply a .jrxml file, Spring compiles the report for you and caches the compiled form to serve future requests.

Using JasperReports View Classes

Using the JasperReports view classes is just like using any other view class in Spring. You have to link a Controller to the URLs behind which you want the reports to sit, populate a ModelAndView object with the report data and the name of the view, and link the view name to the report file. If you are unfamiliar with these concepts, refer to 18 before continuing with this section.

Building a Controller to handle requests for reports is easy, as shown in Listing D-22.

Listing D-22: Creating a Controller to Handle Report Requests

image from book
package com.apress.prospring.apd.jasperreports;      import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;      import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;      import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;      import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController;      public class ReportController extends MultiActionController {          public ModelAndView handleSimpleReportPdf(HttpServletRequest request,             HttpServletResponse response) throws Exception {         return new ModelAndView("simpleReportPdf", getModel());     }          public ModelAndView handleSimpleReportHtml(HttpServletRequest request,             HttpServletResponse response) throws Exception {              return new ModelAndView("simpleReportHtml", getModel());     }          public ModelAndView handleSimpleReportCsv(HttpServletRequest request,             HttpServletResponse response) throws Exception {              return new ModelAndView("simpleReportCsv", getModel());     }          public ModelAndView handleSimpleReportExcel(HttpServletRequest request,             HttpServletResponse response) throws Exception {              return new ModelAndView("simpleReportExcel", getModel());     }          private Map getModel() {         Map model = new HashMap();         model.put("ReportTitle", "My Demo Report!");         model.put("dataSource", new JRBeanCollectionDataSource(getData()));              return model;     }          private List getData() {         List list = new ArrayList();              for (int x = 0; x < 10; x++) {             ReportBean bean = new ReportBean();             bean.setId(x);             bean.setName("Rob Harrop");             bean.setStreet("1 Some Street");             bean.setCity("Manchester");                  list.add(bean);         }              return list;     } }
image from book

Here you can see that we create four handleXXX() methods that correspond to requests for reports in the four different output formats supported by JasperReports. The interesting part of this code is how we construct the model data. JasperReports allows you to pass in two kinds of data to the report: parameter data and report data.

Parameter data is just general data and is used to customize the overall output of the report, but it has nothing to do with the actual data itself. In Listing D-22, you can see that we add an entry to the model Map called ReportTitle. Behind the scenes, Spring passes this parameter to JasperReports when filling the report with data.

Report data is the actual data the report is created to display. Report data in JasperReports is represented by an instance of a class that implements the JRDataSource interface. In this case, we use the JRBeanCollectionDataSource, which is used to wrap a Collection of JavaBeans where each bean in the Collection corresponds to a single row of output in the report and the properties of each bean correspond to the fields defined in the report. This JRDataSource is added to the model data along with the parameter data. Behind the scenes, Spring picks up this JRDataSource instance and passes it to JasperReports when filling the data.

Listing D-23 shows the ApplicationContext configuration for the ReportController class.

Listing D-23: Configuring the ReportController Class

image from book
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>     <bean              >         <property name="basename">             <value>views</value>         </property>     </bean>          <bean                  >         <property name="mappings">             <props>                 <prop key="/simpleReportPdf.pdf">controller</prop>                 <prop key="/simpleReportHtml.html">controller</prop>                 <prop key="/simpleReportCsv.csv">controller</prop>                 <prop key="/simpleReportExcel.xls">controller</prop>             </props>         </property>     </bean>          <bean                  >         <property name="methodNameResolver">             <ref local="resolver"/>         </property>     </bean>          <bean                  symbol">¿                            PropertiesMethodNameResolver">         <property name="mappings">             <props>                 <prop key="/simpleReportPdf.pdf">handleSimpleReportPdf</prop>                 <prop key="/simpleReportHtml.html">handleSimpleReportHtml</prop>                 <prop key="/simpleReportCsv.csv">handleSimpleReportCsv</prop>                 <prop key="/simpleReportExcel.xls">handleSimpleReportExcel</prop>             </props>         </property>     </bean> </beans> 
image from book

You should be more than familiar with this configuration by now. Notice, however, that we use ResourceBundleViewResolver to map the request URLs to actual view classes using the data contained in the views.properties file, shown in Listing D-24.

Listing D-24: Mapping Requests to View Classes

image from book
simpleReportPdf.class=¿ org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView simpleReportPdf.url=/WEB-INF/reports/DataSourceReport.jasper      simpleReportHtml.class=¿ org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView simpleReportHtml.url=/WEB-INF/reports/DataSourceReport.jasper      simpleReportCsv.class=¿ org.springframework.web.servlet.view.jasperreports.JasperReportsCsvView simpleReportCsv.url=/WEB-INF/reports/DataSourceReport.jasper      simpleReportExcel.class=¿ org.springframework.web.servlet.view.jasperreports.JasperReportsExcelView simpleReportExcel.url=/WEB-INF/reports/DataSourceReport.jasper
image from book

Notice that each report format has a corresponding view class, which encapsulates rendering behavior for that format. To test this example out, deploy it into your servlet container—we used Tomcat—and point your browser at http://localhost:8080/jasper/ simpleReportPdf.pdf to bring up the filled report in PDF format.

Using JasperReports Outside the Web Tier

Spring support for JasperReports is not just constrained to the Web Tier. Using the JasperReportsUtils class, you can simplify the use of JasperReports in non-web applications with a variety of convenience methods for compiling, filling, and rendering reports in all four report formats. The JasperReportsUtils class provides methods for all of the operations performed by the view classes—indeed, the view classes are implemented using JasperReportsUtils.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net