`
Ivan_Pig
  • 浏览: 381976 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

JasperReports Web Application Sample (翻译)

阅读更多

Web Application Sample

This sample Web application was created to show that the JasperReports library is perfect for using in Web environment.
这个web示例说明了JasperReports能够胜任web环境开发。

You can see here how to generate reports on the server side using Java Server Pages or Java Servlets and how to view them on the client side, either by using Java Applets or by exporting them to PDF, HTML, XLS or RTF format.
你能看见如何在服务端用jsp或servlet来发布报表和如何在客户端查看,可以使用applets或pdf,html,xsl,ptf格式都可以。


Attention! Try the application menu options in the same order in which they are displayed. Otherwise, important steps involved when working with the JasperReports library might get skipped and error messages will be displayed, instead of the expected output.
注意!按照标签显示的顺序来执行他们。否则,关键步骤被跳过的话可能就会报错,而不会出现期望的结果了。

JRXML Report Design Compilation

Note that in the majority of cases, the JRXML report template compilation process is an application build-time process and not a runtime process.
注意了,在多数情况下,JRXML格式的报表模板的编译都是在应用程序的创建时候进行的而不是在运行时进行的


Normally, report templates have to be compiled only once, just like we compile Java source files and then we deploy them with the application. If the report templates are not dynamic, there is no point on recompiling them at runtime. Report definitions are static and only the data supplied to them is different with every report filling operation.
通常情况下,报表模板只需要编译一次,就像我们编译java源代码一样。然后我们把它们发布到应用程序上去。如果报表模板不是动态的,在运行时他们不会再编译一次了。报表时静态的,而变得只是填充的数据。


However, sometimes applications do require runtime report template compilation, so here you can see how this can be achieved in Web environment.
但是,有时候应用程序需要在运行时编译报表模板,所以这里你能看见在web环境下是如何完成编译的。


The following example performs the compilation of the WebappReport.jrxml file and produces the WebappReport.jasper file.
下面的例子编译WebappReport.jrxml文件并产生WebappReport.jasper文件。

jsp 代码
  1. <%@ page import="net.sf.jasperreports.engine.*,net.sf.jasperreports.engine.util.JRProperties" %>   
  2.   
  3. <%   
  4.     JasperCompileManager.compileReportToFile(application.getRealPath("/reports/WebappReport.jrxml"));   
  5. %>   

 

Filling Reports with Data

The servlet and the JSP below both show how a compiled report template (.jasper file) could be filled with data. They place the resulting net.sf.jasperreports.engine.JasperPrint object onto the HTTP session from where subsequent report viewing and exporting servlets and JSPs in this sample application will reuse it.
下面的servlet和jsp都显示了如何向编译过的报表模板里填充数据。填充数据后会获得net.sf.jasperreports.engine.JasperPrint 类型的结果。将其放到HTTP session里。在后面的viewing和exporting里还会用到。

jsp 代码
  1. <%@ page import="datasource.*" %>   
  2. <%@ page import="net.sf.jasperreports.engine.*" %>   
  3. <%@ page import="net.sf.jasperreports.engine.util.*" %>   
  4. <%@ page import="net.sf.jasperreports.engine.export.*" %>   
  5. <%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>   
  6. <%@ page import="java.util.*" %>   
  7. <%@ page import="java.io.*" %>   
  8.   
  9. <%   
  10.     String reportFileName = application.getRealPath("/reports/WebappReport.jasper");   
  11.     File reportFile = new File(reportFileName);   
  12.     if (!reportFile.exists())   
  13.         throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");   
  14.   
  15.     Map parameters = new HashMap();   
  16.     parameters.put("ReportTitle""Address Report");   
  17.     parameters.put("BaseDir", reportFile.getParentFile());   
  18.                    
  19.     JasperPrint jasperPrint =    
  20.         JasperFillManager.fillReport(   
  21.             reportFileName,    
  22.             parameters,    
  23.             new WebappDataSource()   
  24.             );   
  25.                    
  26.     session.setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);   
  27. %>   

 

Exporting Reports to Other Formats Like PDF, HTML, XLS and RTF

Before testing the exporting functionality exposed on this page, the report has to be already filled using the fill report menu option so that the generated net.sf.jasperreports.engine.JasperPrint be available on the HTTP session for exporting.
在测试将报表显示到页面上之前,需要先fill report以得到net.sf.jasperreports.engine.JasperPrint并将其保存到HTTP session中。 

jsp 代码
  1. <%@ page import="datasource.*" %>   
  2. <%@ page import="net.sf.jasperreports.engine.*" %>   
  3. <%@ page import="net.sf.jasperreports.engine.util.*" %>   
  4. <%@ page import="net.sf.jasperreports.engine.export.*" %>   
  5. <%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>   
  6. <%@ page import="java.util.*" %>   
  7. <%@ page import="java.io.*" %>   
  8.   
  9. <%   
  10.     File reportFile = new File(application.getRealPath("/reports/WebappReport.jasper"));   
  11.     if (!reportFile.exists())   
  12.         throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");   
  13.   
  14.     JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());   
  15.   
  16.     Map parameters = new HashMap();   
  17.     parameters.put("ReportTitle""Address Report");   
  18.     parameters.put("BaseDir", reportFile.getParentFile());   
  19.                    
  20.     JasperPrint jasperPrint =    
  21.         JasperFillManager.fillReport(   
  22.             jasperReport,    
  23.             parameters,    
  24.             new WebappDataSource()   
  25.             );   
  26.                    
  27.     JRHtmlExporter exporter = new JRHtmlExporter();   
  28.   
  29.     StringBuffer sbuffer = new StringBuffer();   
  30.   
  31.     session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);   
  32.        
  33.     exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);   
  34.     exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);   
  35.     exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");   
  36.        
  37.     exporter.exportReport();   
  38. %>   
分享到:
评论
1 楼 liangzedong 2011-01-20  
哥,这个好像没有完哦!能不能全部放上去啊

相关推荐

Global site tag (gtag.js) - Google Analytics