当前位置: 首页 > 工具软件 > BIRT > 使用案例 >

birt

戚育
2023-12-01

和现有项目有两种结合方式。
其一:直接使用birt_runtime_2.1.2中的WebViewerExample。
将其发布到C:/jakarta-tomcat-5.0.28/webapps目录下,并将其更改名称为birtApp。
在birtApp /WEB-INF/platform/plugins/com.lowagie.itext目录下新建文件夹lib,并将itext-1.3.jar和iTextAsian.jar拷入其中。
将已完成的报表,比如test.rptdesign放到birtApp的根目录下,即可通过
http://localhost:8080/birt/frameset?__report=test.rptdesign(相对路径)
或者
http://localhost:8080/birt/frameset?__report=C:/jakarta-tomcat-5.0.28/webapps/birt/test.rptdesign(绝对路径)
来访问。
若报表设置以后参数,则只需在上述url后拼接参数及值即可。
Eg: http://localhost:8080/birt/frameset?__report=test.rptdesign&sample=my+parameter
其二:在我们的程序中使用birt提供的Report Engine Api来调用报表并展现报表。
新建web项目webrpt。
在webrpt的web模块下的WEB-INF目录中新建文件夹lib,并将birt-runtime-2_1_2/ReportEngine/lib下的所有文件拷入其中。
如上,同样在WEB-INF目录中新建文件夹platform,并将birt-runtime-2_1_2/ReportEngine下的plugins和configuration拷入其中。
将itext-1.3.jar和iTextAsian.jar拷入platform/plugins/com.lowagie.itext文件夹的lib目录中(此处如果没有该目录,新建之),亦或者将所述jar包拷入WEB-INF/lib目录下也可。
在webrpt的web模块的根目录下新建images和reports文件夹。其中reports文件夹中放
已开发完成的报表文件。
当然所使用的数据库驱动也要放在lib文件夹下。
使用report engine api开发调用及展示报表的相关程序。代码如下:
WebReport.java
package com.lisa;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.IReportEngine;


public class WebReport extends HttpServlet {

/**
 *
 */
private static final long serialVersionUID = 1L;
/**
 * Constructor of the object.
 */
private IReportEngine birtReportEngine = null;
protected static Logger logger = Logger.getLogger( "org.eclipse.birt" );

public WebReport() {
 super();
}

/**
 * Destruction of the servlet.
 */
public void destroy() {
 super.destroy();
 BirtEngine.destroyBirtEngine();
}


/**
 * The doGet method of the servlet.
 *
 */
public void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
    System.out.println("doGet................");

 //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");
 System.out.println("reportName:::"+reportName);
 ServletContext sc = req.getSession().getServletContext();
 this.birtReportEngine = BirtEngine.getBirtEngine(sc);
 
 //setup image directory
 HTMLRenderContext renderContext = new HTMLRenderContext();
 renderContext.setBaseImageURL(req.getContextPath()+"/images");
 renderContext.setImageDirectory(sc.getRealPath("/images"));
 
 logger.log( Level.FINE, "image directory " + sc.getRealPath("/images")); 
 System.out.println("stdout image directory " + sc.getRealPath("/images"));
 
 HashMap contextMap = new HashMap();
 contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
 
 IReportRunnable design;
 try
 {
  //Open report design
  design = birtReportEngine.openReportDesign( sc.getRealPath("/Reports")+"/"+reportName );
  //create task to run and render report
  IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design ); 
  task.setAppContext( contextMap );
 
  HashMap paramMap=new HashMap();
  paramMap.put("param","%");
  paramMap.put("sample", "lisa ok");
 
  task.setParameterValues(paramMap);


 
  //set output options
  HTMLRenderOption options = new HTMLRenderOption();
 //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
 options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
  options.setOutputStream(resp.getOutputStream());
  task.setRenderOption(options);
 
  //run report
  task.run();
  task.close();
 }catch (Exception e){
 
  e.printStackTrace();
  throw new ServletException( e );
 }
}

/**
 * The doPost method of the servlet.
 *
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

 response.setContentType("text/html");
 PrintWriter out = response.getWriter();
 out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
 out.println("<HTML>");
 out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
 out.println("  <BODY>");
 out.println(" Post does nothing");
 out.println("  </BODY>");
 out.println("</HTML>");
 out.flush();
 out.close();
}

/**
 * Initialization of the servlet.
 *
 * @throws ServletException if an error occure
 */
public void init() throws ServletException {
 System.out.println("init...................");
 BirtEngine.initBirtConfig();
 
}

}

BirtEngine.java
package com.lisa;

import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;

import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.IReportEngine;
import javax.servlet.*;
import org.eclipse.birt.core.framework.PlatformServletContext;
import org.eclipse.birt.core.framework.IPlatformContext;
import  org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;

public class BirtEngine {

private static IReportEngine birtEngine = null;

private static Properties configProps = new Properties();

private final static String configFile = "com/lisa/BirtConfig.properties";

public static synchronized void initBirtConfig() {
 loadEngineProps();
}

public static synchronized IReportEngine getBirtEngine(ServletContext sc) {
 if (birtEngine == null)
 {
  EngineConfig config = new EngineConfig();
  if( configProps != null){
   String logLevel = configProps.getProperty("logLevel");
   Level level = Level.OFF;
   if ("SEVERE".equalsIgnoreCase(logLevel))
   {
    level = Level.SEVERE;
   } else if ("WARNING".equalsIgnoreCase(logLevel))
   {
    level = Level.WARNING;
   } else if ("INFO".equalsIgnoreCase(logLevel))
   {
    level = Level.INFO;
   } else if ("CONFIG".equalsIgnoreCase(logLevel))
   {
    level = Level.CONFIG;
   } else if ("FINE".equalsIgnoreCase(logLevel))
   {
    level = Level.FINE;
   } else if ("FINER".equalsIgnoreCase(logLevel))
   {
    level = Level.FINER;
   } else if ("FINEST".equalsIgnoreCase(logLevel))
   {
    level = Level.FINEST;
   } else if ("OFF".equalsIgnoreCase(logLevel))
   {
    level = Level.OFF;
   }

   config.setLogConfig(configProps.getProperty("logDirectory"), level);
  }

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


  try
  {
   Platform.startup( config );
  }
  catch ( BirtException e )
  {
   e.printStackTrace( );
  }

  IReportEngineFactory factory = (IReportEngineFactory) Platform
  .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
  birtEngine = factory.createReportEngine( config );


 }
 return birtEngine;
}

public static synchronized void destroyBirtEngine() {
 if (birtEngine == null) {
  return;
 } 
 birtEngine.shutdown();
 Platform.shutdown();
 birtEngine = null;
}

public Object clone() throws CloneNotSupportedException {
 throw new CloneNotSupportedException();
}

private static void loadEngineProps() {
    System.out.println("loadEngineProps.....................");
 try {
  //Config File must be in classpath
  ClassLoader cl = Thread.currentThread ().getContextClassLoader();
  InputStream in = null;
  in = cl.getResourceAsStream (configFile);
  configProps.load(in);
  in.close();


 } catch (IOException e) {
  e.printStackTrace();
 }

}

}
BirtConfig.properties
logDirectory=c:/temp
logLevel=FINEST

通过web页面输入要访问的报表名称,并提交到处理请求的相关servlet.
Test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
  <head>
  </head>
 
  <body>
    <form name="myform" action="./webReport" method="get">
    <table>
    <tr>
    <td>报表名称(带后缀名)</td>
    <td><input name="ReportName" type="text"></td>
    <td><button type="submit">查看</button></td>
    </tr>
    </table>
    </form>
  </body>
</html>
修改web.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>WebReport</servlet-name>
    <servlet-class>com.lisa.WebReport</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>WebReport</servlet-name>
    <url-pattern>/webReport</url-pattern>
  </servlet-mapping>
</web-app>

 类似资料: