Expert Spring MVC and Web Flow

(Dana P.) #1

Spring’s Jasper support consists of five concrete Viewimplementations for each of the
final output types available. These were shown in Figure 7-1 in the previous chapter and are
recapped here.



  • JasperReportsCsvView

  • JasperReportsHtmlView

  • JasperReportsPdfView

  • JasperReportsXlsView

  • JasperReportsMultiFormatView


The first four produce fixed format reports in CSV, HTML, PDF, and Excel format, respec-
tively; the final one allows the format to be decided at runtime. A new, concrete type called
ConfigurableJasperReportsViewis likely to be available in Spring 1.3, which enables you to
specify which JRExporterto use.
To use one of the Jasper Viewtypes, you specify the familiar configuration in the
ViewResolver’s configuration file. Listing 8-43 shows an example that will display the home
page model from our sample application as a Jasper PDF report.


Listing 8-43.views.properties Settings for the Jasper Home Page


home.class=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
home.url=/WEB-INF/jasper/home.jrxml


The file home.jrxmlis the report design itself. Jasper reports are natively XML, which is
great if you need to get in and do low-level editing of a report, but coding one by hand can be
quite laborious. A few GUI designers are available that will read and write Jasper’s report
design files, some of which are licensed commercially.


■Tip Although you can deploy the design file as a .jrxmljust as we’re showing here, Jasper needs to
compile the report prior to use. Similar to JSPs, it will do this the first time it encounters a new report and
then cache the resultant .jasperfile. To do this, however, you need to make the jdt-compiler.jar
available to your application and configure the JRCompiler accordingly. As such, it’s recommended only for
development environments. For production, you can precompile your reports using an Ant task provided with
Jasper and deploy the compiled versions instead.


Multiformat View


The JasperReportsMultiFormatViewis a thin wrapper that delegates to one of the other report
types at runtime. A mapping key is specified that relates to the class name of the actual report
type required.
In Listing 8-44, we show an example of how this can be used in the Controller. The
Controllerextracts the filename extension from the incoming request URI and uses it to
tell Jasper which report format should be used.


CHAPTER 8 ■SUPPORTED VIEW TYPES 261
Free download pdf