Oracle神谕

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  284 随笔 :: 9 文章 :: 106 评论 :: 0 Trackbacks

13.7. JasperReports
JasperReports (http://jasperreports.sourceforge.net) is a powerful, open-source reporting engine that supports the creation of report designs using an easily understood XML file formats. JasperReports is capable of rendering reports output into four different formats: CSV, Excel, HTML and PDF.

13.7.1. Dependencies (依赖)
Your application will need to include the latest release of JasperReports, which at the time of writing was 0.6.1. JasperReports itself depends on the following projects:

BeanShell

Commons BeanUtils

Commons Collections

Commons Digester

Commons Logging

iText

POI

JasperReports also requires a JAXP compliant(适应的) XML parser.

13.7.2. Configuration(配置)
To configure JasperReports views in your ApplicationContext you have to define a ViewResolver to map view names to the appropriate view class depending on which format you want your report rendered in.

13.7.2.1. Configuring the ViewResolver
Typically, you will use the ResourceBundleViewResolver to map view names to view classes and files in a properties file

<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basename">
        <value>views</value>
    </property>
</bean>
                 
Here we've configured an instance of ResourceBundleViewResolver which will look for view mappings in the resource bundle with base name views. The exact contents of this file is described in the next section.

13.7.2.2. Configuring the Views
Spring contains five different View implementations for JasperReports four of which corresponds to one of the four output formats supported by JasperReports and one that allows for the format to be determined at runtime:

JasperReport View Class
1.JasperReportsView           CSV
2.JasperReportsHtmlView       HTML
3.JasperReportsPdfView        PDF
4.JasperReportsXlsView        EXCEL
5.JasperReportsMutiFormatView

Mapping one of these classes to a view name and a report file is simply a matter of adding the appropriate entries into the resource bundle configured in the previous section as shown here:

simpleReport.class=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
simpleReport.url=/WEB-INF/reports/DataSourceReport.jasper
             
Here you can see that the view with name, simpleReport, is mapped to the JasperReportsPdfView class. This will cause the output of this report to be rendered in PDF format. The url property of the view is set to the location of the underlying report file.


13.7.2.3. About Report Files
JasperReports has two distinct types of report file: the design file, which has a .jrxml extension, and the compiled report file, which has a .jasper extension. Typically, you use the JasperReports Ant task to compile your .jrxml design file into a .jasper file before deploying it into your application. With Spring you can map either of these files to your report file and Spring will take care of compiling the .jrxml file on the fly for you. You should note that after a .jrxml file is compiled by Spring, the compiled report is cached for the life of the application. To make changes to the file you will need to restart your application.

JasperReports拥有两种不同的类型的报表文件:设计文件,它是一个拥有.jrxml 扩展的文件,和编译好的报表文件。一般,你使用ant任务在你部署到你的程序中之前来编译你的.jrxml设计报表文件。使用Spring 你可以影射这些文件中的任一到你的报表文件,Spring将会为你在空闲时照顾编译.jrxml文件。 你应当注意在一个.jrxml文件被编译之后,这个编译的报表是被缓存的在你的application生命周期中。如果这些文件修改了,你需要重新启动的你的程序。

13.7.2.4. Using JasperReportsMultiFormatView  使用JasperReportsMutiFormatView
The JasperReportsMultiFormatView allows for report format to be specified at runtime. The actual rendering of the report is delegated to one of the other JasperReports view classes - the JasperReportsMultiFormatView class simply adds a wrapper layer that allows for the exact implementation to be specified at runtime.

JasperReportsMutilFormatView允许你在运行时期指定报表的格式。报表的实际的表现是为委托到JasperReports 视图类的中的一个--JasperMutilFormatView类简单的加了一个包装层允许在运行时期正确的实现被指定。

The JasperReportsMultiFormatView class introduces two concepts: the format key and the discriminator key. The JasperReportsMultiFormatView class uses the mapping key to lookup the actual view implementation class and uses the format key to lookup up the mapping key. From a coding perspective you add an entry to your model with the formay key as the key and the mapping key as the value, for example:

 

public ModelAndView handleSimpleReportMulti(HttpServletRequest request,
HttpServletResponse response) throws Exception {

  String uri = request.getRequestURI();
  String format = uri.substring(uri.lastIndexOf(".") + 1);

  Map model = getModel();
  model.put("format", format);

  return new ModelAndView("simpleReportMulti", model);
}
In this example, the mapping key is determined from the extension of the request URI and is added to the model under the default format key: format. If you wish to use a different format key then you can configure this using the formatKey property of the JasperReportsMultiFormatView class.

By default the following mapping key mappings are configured in JasperReportsMultiFormatView:
Table 13.3. JasperReportsMultiFormatView Default Mapping Key Mappings

Mapping Key View Class
csv   JasperReportsCsvView
html  JasperReportsHtmlView
pdf   JasperReportsPdfView
xls   JasperReportsXlsView

So in the example above a request to URI /foo/myReport.pdf would be mapped to the JasperReportsPdfView class. You can override the mapping key to view class mappings using the formatMappings property of JasperReportsMultiFormatView.

posted on 2005-06-29 10:24 java世界畅谈 阅读(3450) 评论(0)  编辑  收藏 所属分类: Spring

只有注册用户登录后才能发表评论。


网站导航: