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世界暢談 閱讀(3462) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 定西市| 乌审旗| 英山县| 松滋市| 广河县| 文化| 拉萨市| 龙胜| 德阳市| 利津县| 阿尔山市| 溧水县| 台中市| 新密市| 交口县| 泸溪县| 大同市| 海阳市| 广南县| 江山市| 马山县| 留坝县| 祁门县| 古丈县| 水富县| 高雄市| 报价| 敖汉旗| 康保县| 五台县| 临城县| 崇文区| 陇南市| 宝兴县| 江门市| 洞头县| 元江| 衡南县| 怀宁县| 葵青区| 通城县|