BIRT 集成 servlet

总结一下项目中使用过的两种BIRT web 部署的方法:

一:独立部署Birt Web Viewer应用,通过URL访问,生成报表。

二:完整集成Birt Web到现有系统中,使用servlet、API直接调用。 

 

一、独立Birt Web应用的方式:

      对于初次接触Birt的人,使用这种方式无疑是最快的方式。在敏式开发热捧的时代,还有什么比能让系统立马跑起来更让人兴奋的呢?是驴是马,赶紧拉出来溜溜!(实际上我们也是这么做的) 

  

      作为一个开源的项目,BIRT已很无私地为我们做了很多(感动中)。你只需要到Birt官网上下载Runtime包,里边的WebViewerExample包即是一个完整的Web应用。使用eclipse导入web 工程即可,

      目录如下如(figure_1):

 

 BIRT 集成 servlet_第1张图片

      在/report/files中存放设计好的报表模板(怎么设计模板见附录1)。web 应用发布到 tomcat 启动之后即可通过URL,进行访问了。

例如:http://127.0.0.1:8080/report/preview?__report=report/files/gz_hard_count.rptdesign&__format=html

 

       采用上述这种方式,能方便地在现有的Web上嵌入Birt报表,只要在现有系统中通过URL调用就可以了。简单、实用!我们的项目中也一直是这么使用的。虽然中间遇到了一些处理“中国式”报表的格式问题,但通过XSLT转换等其他办法也能克服了。直到最近,在使用图表的报表的时候,保存下的报表文件中图片不能正常显示。详见http://www.birthome.cn/read.php?tid-2160.html

 

       也正是这次这个问题,让我下定决心要改变Birt 的部署方式。查看birt preview servlet 生成的图表源文件可以发现,图片是一个链接。而这个链接是birt preview servlet 自己维护的,图片自动生成在根据session 来来区分的文件夹内。这样就导致,保存下来的图表文件不能再次访问(如下所示)。我们知道,BIRT API 提供 RunAndRenderTask 生成文件,并指定图表文件存放路径。如果能直接调用API,遇到的问题也就迎刃而解了。

<img id="AUTOGENBOOKMARK_43" src="/report/preview?__imageid=custom180b22e1306d2c28f26.png" alt="" style="width: 871.5pt; height: 372.75pt;"></img>

 

 

 

二:集成Birt servlet.

      为了充分发挥Birt 丰富的AJAX渲染效果,有必要完整集成Birt 的servlet,为我所用。下面就详细介绍下Birt 的部署方式。

 

      1:在WebReport/WEB-INF/lib中新建birt-lib文件夹,用于存放Birt 所需jar

      2:拷贝birt-runtime-2_1_0/ReportEngine/lib下的所有jar到 1中建的文件夹

      3:拷贝birt-runtime-2_1_0/Report Engine/plugins 以及 birt-runtime-2_1_0/ReportEngine/configurationWEB-INF

      4:创建reports、images目录用于存放报表模板文件及图片

      5: 拷贝配置文件到WEB-INF下,jrun.web.xml、server-config.wsdd、viewer.properties。添加Brit servlet到web.xml中。

 

      做完上述步骤之后,你的应用的目录结构大致如下图(figure_2)

 

       之后您可以自定义servlet,启动birt platform,并调用Birt api来运行birt 模板并生成文件到指定路径。

       2.1 编写BirtEngine.java,装载birt platform,创建birtEngine

       2.1.1 装载birt plugins:

config.setEngineHome(""); IPlatformContext context = new PlatformServletContext( sc ); config.setPlatformContext( context );

       启动platform,创建birt Engine

public static synchronized IReportEngine getBirtEngine(ServletContext sc) { if (birtEngine == null) { . . try { //Start up the OSGi framework Platform.startup( config ); } catch ( BirtException e ) { e.printStackTrace( ); } IReportEngineFactory factory = (IReportEngineFactory) Platform. createFactoryObject( IReportEngineFactory. EXTENSION_REPORT_ENGINE_FACTORY ); birtEngine = factory.createReportEngine( config ); } return birtEngine; } }

 

       2.2 自定义servlet(WebReport.java),创建RunAndRenderTask 调用报表模板,并生成文件到指定目录。

       2.2.1:init方法装载birt platform

public void init() throws ServletException { BirtEngine.initBirtConfig(); }

       2.2.2:destory方法,关闭服务时关闭birt platform

public void destroy() { super.destroy(); //call engine and platform shutdown BirtEngine.destroyBirtEngine(); }

      2.2.3:doGet 方法,调用报表模板并生成文件到指定目录

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // get report name and launch the engine resp.setContentType("text/html"); // resp.setContentType( "application/pdf" ); // resp.setHeader ("Content-Disposition","inline; filename=test.pdf"); String reportName = req.getParameter("ReportName"); ServletContext sc = req.getSession().getServletContext(); this.birtReportEngine = BirtEngine.getBirtEngine(sc); // setup image directory HTMLRenderContext renderContext = new HTMLRenderContext(); renderContext.setBaseImageURL(req.getContextPath() + "/report/images"); renderContext.setImageDirectory(sc.getRealPath("/report/images")); logger.log(Level.FINE, "image directory " + sc.getRealPath("/report/images")); System.out.println("stdout image directory " + sc.getRealPath("/report/images")); HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>(); contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext); IReportRunnable design; try { // Open report design design = birtReportEngine.openReportDesign(sc .getRealPath("/report/files") + "/" + reportName); // create task to run and render report IRunAndRenderTask task = birtReportEngine .createRunAndRenderTask(design); task.setAppContext(contextMap); // set output options HTMLRenderOption options = new HTMLRenderOption(); // options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF); // options.setOutputStream(resp.getOutputStream()); String savePath = "D:/works/workTools/apache-tomcat-6.0.16/webapps/reportOutPutPath"; // options.setOutputFileName("../output.html"); String fileName = savePath + File.separator + "output.html"; System.out.println("fileName is : " + fileName); OutputStream fout = new FileOutputStream(new File(fileName)); options.setOutputStream(fout); options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); // options.setOutputFormat("HTML"); options.setImageDirectory("/report/images"); options.setEnableAgentStyleEngine(true); options.setEmbeddable(true); task.setRenderOption(options); // run report task.run(); ServletOutputStream out = resp.getOutputStream(); out.println("done ok!!!!"); out.close(); fout.close(); task.close(); } catch (Exception e) { e.printStackTrace(); throw new ServletException(e); } }

       2.2.4: 配置servlet,调用即可。例如http://localhost:8080/report/run?ReportName=testWebReport.rptdesign

<servlet> <servlet-name>CreateWebReport</servlet-name> <servlet-class>hob.integration.birt.WebReport</servlet-class> </servlet> <servlet-mapping> <servlet-name>CreateWebReport</servlet-name> <url-pattern>/creatCharReport</url-pattern> </servlet-mapping>

 

参考:

        1:BIRT runtime 包下载路径 http://download.eclipse.org/birt/downloads/

        2:BIRT 图表显示异常 http://www.birthome.cn/read.php?tid-2160.html

        3:BIRT 部署 by Jason Weathersby 07/26/2006  http://onjava.com/pub/a/onjava/2006/07/26/deploying-birt.html?page=2

        4:birt 中文手册 http://download.csdn.net/source/3318430

你可能感兴趣的:(Web,image,String,servlet,report,报表)