在網上查了下BIRT的文章,不是很多。而且講述的也不是十分全面。還是得自己琢磨啊。
剛實驗好,貼上來分享一下共同交流。希望也能幫助到對API部署還困惑的人。
做這個純粹是看官網上的例子,還有好多疑點,希望高手能給我解答下。
首先,這個API部署其實在官網上是叫"Servlet Example"。我沒有在網上找到中文的這個教程,所以我就以我這貧乏的英語水品來自己理解了官網上的步驟。下面直接開始,我不是原文翻譯的所以有些地方用詞不當也是正常。
既然是個Servlet Exampl那么它肯定符合Java EE的規范。我先把目錄結構貼到下面,讓大家先有個整體感覺。

圖片不是很清楚,湊合著看吧。
我先說下目錄結構Tomcat和Webapps就不說了。下面就是自己新建一個自己的文件夾在tomcat/webapps下面是自己的API BIRT的項目。名字自己起就好了,官網上是叫WebReport吧,上面寫的很清楚了。我自己是叫TestAPI
Images和Reports是用來放報表的地方,Images是圖片和圖像報表,然后Reports是文字報表。就是把自己建好的報表copy到 Reports里面。具體建報表就不說了,自帶的幫助文檔和網上有很多資料。下面就是WEB-INF目錄了,建立好這個目錄,你的API所有配置和用的資源才可以發動。在WEB-INF下面的lib沒用過也知道是放jar的地方。還有一個platform也是放一些報表運行所需的配置信息和一些jar。其實我們都知道上面圖其實應該在WEB-INF下面還有個classes文件夾,用來放編譯好的java文件。這也是個重要的目錄。上面沒有畫。
官網上是分了五步,我這里就按自己的來了,也不說分步了。
首先也是需要一個runtime從http://www.eclipse.org/downloads/download.php?file=/birt/downloads/drops/R-R1-2_1_3-200707051847/birt-runtime-2.1.3.zip你可以下載到這個“runtime”不是很大。下載好以后打開,然后進入ReportEngine目錄把lib里的所有jar包全部拷貝到自己建立的lib下面,如圖

然后開始往platform里面塞東西。把runtime/ReportEngine的兩個文件夾(plugins和configuration)拷貝到你自己的platform里面。
plugins里面也是一些報表所需的jar而configuration里面是什么東西?就是一個config.ini文件,我不知道是干什么用的。
拷完之后如下:

如果你的報表用到數據庫驅動了,必須把數據庫驅動文件拷貝到\WEB-INF\platform\plugins\org.eclipse.birt.report.data.oda.jdbc_2.1.1.v20070705-1847\drivers這個路徑下面,看著長其實不難找。
拷貝完是這樣,貼個圖:

配置就差不多了,就查一個web.xml文件了。
下面是比較重要的東西。有三個文件。是API的核心操作文件。
BirtConfig.properties - Configuration properties for the Engine.
BirtEngine.java - Class used to initialize the Report Engine.
WebReport.java - The servlet that handles report generation on a GET command.
就是上面三個文件是官網的原文,我拷貝過來的。
第一個是構造報表引擎的資源文件。
第二個用來初始化報表引擎的類。
第三個用GET方法來調用報表。
我英文不好,自己理解是這樣的。(四級我都沒考,我覺得找個美國人待上一個月英語水平就上去了)
具體這三個文件的代碼我貼到下面
BirtConfig.properties
logDirectory=c:/temp
logLevel=FINEST
BirtEngine.java
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 = "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()
{

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();
}

}

}

WebReport.java
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
{

//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()+"/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<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("/Reports")+"/"+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_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
{

this.doGet(request,response);


/** *//**
* Initialization of the servlet.
*
* @throws ServletException if an error occure
*/

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

}

官網上還有2.2版本WebReport.java的代碼,我反正沒用上。您要有用上的可以去試試。
這三個文件弄好了就ok了。官網上還羅嗦一大堆是在Eclipse下開發要加什么插件什么插件的其實就是加上Servlet支持的就行。我沒有用純Eclipse開發所以直接編譯好這兩個java,連同拷貝上那個BirtConfig.properties資源文件到自己的WEB-INF/classes下面就行了。

這下好了,就只有配置你的servlet了。
在WEB-INF下面建立一個web.xml文件,配置一下這個webreport的servlet
代碼:
<?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>WebReport</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebReport</servlet-name>
<url-pattern>/webReport</url-pattern>
</servlet-mapping>
</web-app>
最后,把自己做好的報表放到自己項目下面的reports(這是個復數的s)里面就好,這個目錄不能錯的,在servlet里面是要找這個目錄的。
很簡單,然后自己再寫一個自己的jsp就ok了
原來的WebReport.java文件的doPost方法里什么都沒有的,如果自己的jsp通過post傳遞的話不會顯示出來報表。我上面吧post里面調用get了。
這下就全部OK了。
下面我的這個小jsp給大家參考。

<%
@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
</head>

<body>
<form name="theform" action="webReport" method="post">
<table>
<tr>
<td>TestReport.rptdesign</td>
<td><input name="ReportName" type="text"></td>
<td><button type="submit">走你</button></td>
</tr>
</table>
</form>
</body>
</html>
OK在輸入框里面填寫你的報表名稱,記得要帶擴展名。
這就完工了,但是有個問題,我在用腳本數據源的時候提示不成功。還有我的帶參數的報表這樣部署的話不讓輸入參數自己就全出來了。希望大俠能幫我參考下。留言啊。留言~~小弟新人,學習中,渴望進步!明天還得加班。困啊
高手新人都加好友啊:MSN:lewesbonnie@hotmail.com QQ: 232172300
轉載請注明出處和作者哈!!
posted on 2008-06-06 23:08
leweslove 閱讀(2967)
評論(0) 編輯 收藏 所屬分類:
Other