sparta-紫杉 2010-8-18 8:12
開(kāi)發(fā)環(huán)境: eclipse3.4.2, spring3.0, struts2.1.8, hibernate3.3.1, weblogic10.3, oracle9.2, birt2.3.2。
一、安裝Birt到Eclipse
1、直接下載All-ine-one, 即直接下載帶有Birt的Eclipse。該種方法已經(jīng)試驗(yàn)可行。
2、可下載相關(guān)部件,然后安裝到各相關(guān)目錄。該種方法沒(méi)有試過(guò),但可參見(jiàn)網(wǎng)絡(luò)文章“Birt安裝指南”。
3、直接通過(guò)Eclipse的Software Updates安裝Birt插件,參見(jiàn)如下網(wǎng)址:
http://download.eclipse.org/birt/downloads/updmaninst2.2.php
筆者就是通過(guò)第3種方式安裝Birt到Eclipse的。
二、在已有項(xiàng)目中做報(bào)表,報(bào)表文件默認(rèn)為test.rptdesign
1、將報(bào)表文件放到webapp/jsp/report/文件夾下。
當(dāng)然,假設(shè)Birt報(bào)表文件嵌入在jsp中,Birt在找相關(guān)的*.rptdesign文件進(jìn)行顯示時(shí),會(huì)默認(rèn)從webapp文件夾下找。
筆者將*.rptdesign文件放到webapp/jsp/report/文件夾下之后,birt就會(huì)找不到該文件,怎么辦?可以在Web.xml中設(shè)置:
<context-param>
<param-name>BIRT_VIEWER_WORKING_FOLDER</param-name>
<param-value>/jsp/report/</param-value>
</context-param>
2、編輯數(shù)據(jù)源, 數(shù)據(jù)源為“腳本數(shù)據(jù)源 scripted Data Source”,為該數(shù)據(jù)起個(gè)名字為SSH2。
3、編輯數(shù)據(jù)集,該數(shù)據(jù)集和SSH2數(shù)據(jù)源掛接。
1)、數(shù)據(jù)集的open屬性:
factory = new Packages.test.report.BirtFactory.getInstance();
unitsList = factory.getUnitsLst(params["unitsId"].value);
iterator = unitsList.iterator();

注意: 上述BirtFactory類(lèi)在第四標(biāo)題中給出代碼!
2)、數(shù)據(jù)集的fetch屬性:

if(iterator.hasNext() == false)
{
return false;

}else
{
var units = iterator.next();
row["id"] = units.getId();
row["name"] = units.getName();
row["type"] = units.getType();
return true;
}
3)、數(shù)據(jù)集的close屬性:
units = null;
iterator = null;
unitsList = null;
factory=null;
4、本例子為顯示相關(guān)的“單位列表”,因此報(bào)表文件顯示字段為: 單位id, 單位名稱(chēng), 單位類(lèi)型。
5、還必須為報(bào)表文件設(shè)置參數(shù):
1)、設(shè)置報(bào)表參數(shù): 在“Report Parameters”中設(shè)置,本例中為unitsId,默認(rèn)值為10011001。
2)、設(shè)置Data Set參數(shù):
在“Data Set”點(diǎn)右鍵編輯,在彈出的窗口中再點(diǎn)“Parameters”,設(shè)置一個(gè)與報(bào)表參數(shù)同名的參數(shù)unitsId,默認(rèn)值為10011001,要注意同名,要不然,在頁(yè)面上會(huì)找不到相關(guān)的參數(shù)而報(bào)錯(cuò)。
三、將報(bào)表文件嵌入到j(luò)sp

<%
@ taglib uri="/birt.tld" prefix="birt" %>
<!-- 報(bào)表 -->
<div id="reprot">

<birt:viewer id="birtViewer"
reportDesign="testBirtAndSpring.rptdesign" format="HTML"
width="800" height="480" left="0"
top="0" showParameterPage="false">
<birt:param name="unitsId" value="${unitsId}" />
</birt:viewer>
</div>
四、Birt和SSH2集成
1、為了防止Birt在通過(guò)URL輸出顯示報(bào)表時(shí), 其中的frameset被Struts2過(guò)濾掉,須編寫(xiě)下面的自定義過(guò)濾器,當(dāng)然這個(gè)自定義過(guò)濾器不是我寫(xiě)的,是一個(gè)叫做Shoru的人寫(xiě)的:

public class BirtFilter implements Filter
{

/** *//**
* 容器,封裝birt相關(guān)功能的uri和所對(duì)應(yīng)Servlet名的鍵值對(duì)
*/
Map<String, String> map = new HashMap<String, String>();


/** *//**
* Context.
*/
ServletContext context;


/** *//**
* debug開(kāi)關(guān)
*/
static boolean debug = false;


/** *//**
*/

public void destroy()
{
map = null;
}


/** *//**
* 過(guò)濾birt請(qǐng)求,轉(zhuǎn)發(fā)到對(duì)應(yīng)的servlet,以繞過(guò)其他過(guò)濾器,e.g. struts
*
*/
public void doFilter(ServletRequest request, ServletResponse response,

FilterChain fc) throws IOException, ServletException
{

HttpServletRequest req = (HttpServletRequest) request;
String uri = req.getRequestURI();

if (debug)
{
System.out.println(">>>Requesting " + uri + "?"
+ req.getQueryString());
}
Set<String> keys = map.keySet();


for (String key : keys)
{

/**//*
* TODO:這里的判斷只是簡(jiǎn)單地調(diào)用contains方法,這樣就帶來(lái)較多限制。
* 比如工程子目錄的命名、struts命名空間等都受到birtViewer的約束。待改進(jìn)。
*/

if (uri.contains(key))
{
RequestDispatcher rd = this.context.getNamedDispatcher(map
.get(key));

if (rd != null)
{

if (debug)
{
System.out.println(">>>Redirect successfully executed");
}
// 跳過(guò)其他過(guò)濾器,跳轉(zhuǎn)到對(duì)應(yīng)的servlet
rd.forward(request, response);

} else
{

if (debug)
{
System.out
.println(">>>Redirect unsuccessfully executed");
}
}
return;
}
}

// 將請(qǐng)求交給下一個(gè)過(guò)濾器
fc.doFilter(request, response);
}


/** *//**
* @description
* @author Shoru
* @date 2009-8-21
* @version 1.0.0
* @param fc
* @throws ServletException
*/

public void init(FilterConfig fc) throws ServletException
{

this.context = fc.getServletContext();

/**//*
* 這里注意,在項(xiàng)目目錄的命名時(shí),不要取和birt內(nèi)置的一些servlet名重復(fù)。 請(qǐng)根據(jù)項(xiàng)目的web.xml自行配置。
* (包括frameset、run、preview、download、parameter、document、output)
*/
map.put("frameset", "ViewerServlet");
map.put("preview", "EngineServlet");
map.put("report", "ViewerServlet");
}
}
并且需要在Web.xml中進(jìn)行如下配置才生效(并且必須要配置在Struts2的過(guò)濾器的前面):
<filter>
<filter-name>BirtFilter</filter-name>
<filter-class>avatar.base.birt.BirtFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>BirtFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2、和Spring集成,以便利用Spring來(lái)管理Bean、事務(wù)及數(shù)據(jù)源:
編寫(xiě)B(tài)irtFactory類(lèi):

public class BirtFactory
{

private static final Logger logger = Logger.getLogger(BirtFactory.class);

private static BirtFactory instance;
private static ApplicationContext context = WebApplicationContextUtils
.getRequiredWebApplicationContext(InitServlet.SERVLET_CONTEXT);

private final UnitsBuz manager = (UnitsBuz) context.getBean("unitsBuz");

private BirtFactory()
{
logger.debug("Init the instance of BirtFactory
");
}


public static BirtFactory getInstance()
{


if (instance == null)
{

instance = new BirtFactory();
}

return instance;
}


public List<SysUnits> getUnitsLst(String unitId)
{

return manager.findByIdList(unitId);

}
}
編寫(xiě)在BirtFactory中用到的InitServlet類(lèi),以便在web容器啟動(dòng)時(shí),獲得ApplicationContext.xml中相關(guān)上下文參數(shù)。

public class InitServlet extends HttpServlet
{

private static final long serialVersionUID = 1L;

public static ServletContext SERVLET_CONTEXT;


public void init() throws ServletException
{

SERVLET_CONTEXT = getServletContext();
}

}
上述InitServlet需要在Web.xml中進(jìn)行配置:
<!-- Birt和Spring集成的Servlet ,用以Birt可利用Spring完成報(bào)表數(shù)據(jù)源的建立、
數(shù)據(jù)的獲取及事務(wù)的管理功能!10/8/17 22:02 -->
<servlet>
<servlet-name>Birt config</servlet-name>
<servlet-class>test.report.InitServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
五、集成到Web項(xiàng)目
1、拷相關(guān)的文件:
首先從 http://download.eclipse.org/birt/downloads/build_list.php 下載Birt的運(yùn)行時(shí): birt-runtime-2_3_2_2.zip,然后解壓。
復(fù)制birt-runtime-2_3_2\WebViewerExample\下的logs,report,scriptlib,webcontent文件夾到avatar項(xiàng)目的webapp目錄下。
復(fù)制birt-runtime-2_3_2\WebViewerExample\WEB-INF\下的lib,platform,tlds文件夾,及相關(guān)server-config.wsdd,viewer.properties文件到avatar項(xiàng)目的webapp\WEB-INF下面。
將\birt-runtime-2_3_2\ReportEngine\文件夾打包成reportEngine.zip,復(fù)制到avatar\webapp\WEB-INF\lib文件夾下(這個(gè)東西缺了不行)。
2、web.xml配置,好家伙,這個(gè)配置比較繁瑣:
<context-param>
<param-name>BIRT_VIEWER_TIMEZONE</param-name>
<param-value></param-value>
</context-param>
<!-- Report resources directory for preview. Default to ${birt home} -->
<context-param>
<param-name>BIRT_VIEWER_WORKING_FOLDER</param-name>
<param-value>/jsp/report/</param-value>
</context-param>
<!-- The generated document files directory. Default to ${birt home}/documents -->
<context-param>
<param-name>BIRT_VIEWER_DOCUMENT_FOLDER</param-name>
<param-value>/documents</param-value>
</context-param>

<!-- If only access the reprot resources under working folder. Default is true -->
<context-param>
<param-name>WORKING_FOLDER_ACCESS_ONLY</param-name>
<param-value>false</param-value>
</context-param>

<!-- Output image/chart directory. Default to ${birt home}/report/images -->
<context-param>
<param-name>BIRT_VIEWER_IMAGE_DIR</param-name>
<param-value>/jsp/report/</param-value>
</context-param>

<!-- Engine log directory. Default to ${birt home}/logs -->
<context-param>
<param-name>BIRT_VIEWER_LOG_DIR</param-name>
<param-value>/logs</param-value>
</context-param>

<!-- Report engine log level -->
<context-param>
<param-name>BIRT_VIEWER_LOG_LEVEL</param-name>
<param-value>WARNING</param-value>
</context-param>

<!-- Directory to store all birt report script libraries (JARs). Default to ${birt home}/scriptlib -->
<context-param>
<param-name>BIRT_VIEWER_SCRIPTLIB_DIR</param-name>
<param-value>/scriptlib</param-value>
</context-param>
<!-- Resource location directory. Default to ${birt home} -->
<context-param>
<param-name>BIRT_RESOURCE_PATH</param-name>
<param-value></param-value>
</context-param>

<!-- Preview report max rows limited. -->
<context-param>
<param-name>BIRT_VIEWER_MAX_ROWS</param-name>
<param-value>100</param-value>
</context-param>

<!-- Preview report max cube fetch levels limited.(Only support to preview a report design file using preview pattern.) -->
<context-param>
<param-name>BIRT_VIEWER_MAX_CUBE_ROWLEVELS</param-name>
<param-value></param-value>
</context-param>
<context-param>
<param-name>BIRT_VIEWER_MAX_CUBE_COLUMNLEVELS</param-name>
<param-value></param-value>
</context-param>

<!-- Memory size(MB) for creating cube. -->
<context-param>
<param-name>BIRT_VIEWER_CUBE_MEMORY_SIZE</param-name>
<param-value></param-value>
</context-param>
<!-- If always overwrite generated document file. For runtime,efalult to true -->
<context-param>
<param-name>BIRT_OVERWRITE_DOCUMENT</param-name>
<param-value>true</param-value>
</context-param>

<!-- Define BIRT viewer configuration file -->
<context-param>
<param-name>BIRT_VIEWER_CONFIG_FILE</param-name>
<param-value>WEB-INF/viewer.properties</param-value>
</context-param>

<!-- If turn on the function that supports print on the server side. Default to on. -->
<context-param>
<param-name>BIRT_VIEWER_PRINT_SERVERSIDE</param-name>
<param-value>ON</param-value>
</context-param>

<!-- If force optimized HTML output. Default to true -->
<context-param>
<param-name>HTML_ENABLE_AGENTSTYLE_ENGINE</param-name>
<param-value>true</param-value>
</context-param>

<!-- Viewer Filter.Currently, set request character encoding to UTF-8. -->
<filter>
<filter-name>ViewerFilter</filter-name>
<filter-class>org.eclipse.birt.report.filter.ViewerFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>ViewerFilter</filter-name>
<servlet-name>ViewerServlet</servlet-name>
</filter-mapping>

<filter-mapping>
<filter-name>ViewerFilter</filter-name>
<servlet-name>EngineServlet</servlet-name>
</filter-mapping>
<!-- Viewer Servlet Context Listener -->
<listener>
<listener-class>org.eclipse.birt.report.listener.ViewerServletContextListener</listener-class>
</listener>

<!-- Viewer HttpSession Listener -->
<listener>
<listener-class>org.eclipse.birt.report.listener.ViewerHttpSessionListener</listener-class>
</listener>
<!-- Viewer Servlet, Support SOAP -->
<servlet>
<servlet-name>ViewerServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.ViewerServlet</servlet-class>
</servlet>

<!-- Engine Serlvet -->
<servlet>
<servlet-name>EngineServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.BirtEngineServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ViewerServlet</servlet-name>
<url-pattern>/frameset</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ViewerServlet</servlet-name>
<url-pattern>/run</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/preview</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/parameter</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/document</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/output</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/extract</url-pattern>
</servlet-mapping>

<jsp-config>
<taglib>
<taglib-uri>/birt.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/birt.tld</taglib-location>
</taglib>
</jsp-config>
3、struts.xml配置:
<!-- birt報(bào)表-試驗(yàn) ,在試驗(yàn)后刪除。 10/8/17 22:17 -->
<action name="UnitsQry2" class="unitsAct" method="getUnitsList">
<result name="input">jsp/report/unitsBirtAndSpring.jsp</result>
<result name="success">jsp/report/unitsBirtAndSpring.jsp</result>
</action>
4、Action:

/**//*
* 試驗(yàn) Birt 報(bào)表,傳查詢(xún)參數(shù)
*/

public String getUnitsList()
{

if (null == unitsId || "".equals(unitsId))
{
unitsId = "10011001";
}
return SUCCESS;
}
5、業(yè)務(wù)邏輯Buz: 執(zhí)行dao.getUnitsList(String unitsId)
6、Model:?jiǎn)挝槐淼腜ojo。
7、Dao: 通過(guò)執(zhí)行g(shù)etHibernateTemplage().getUnitsList(String unitsId)提取相應(yīng)數(shù)據(jù)(當(dāng)然了,你得確保數(shù)據(jù)庫(kù)里面有數(shù)據(jù)才可以)。
8、applicationContext.xml:
注入U(xiǎn)nitsModel、注入業(yè)務(wù)邏輯組件、注入SessionFactory。
六、運(yùn)行
在游覽器中運(yùn)行Struts2的Action, 如下URL:
http://192.268.0.72:7001/webapp/UnitsQry2
若成功,就會(huì)顯示相關(guān)結(jié)果,如下截圖:

七、常見(jiàn)問(wèn)題歸納
1、提示如下錯(cuò)誤: org.eclipse.birt.report.exception.ViewerException: There is no report design object available.
<!-- If force optimized HTML output. Default to true -->
<context-param>
<param-name>HTML_ENABLE_AGENTSTYLE_ENGINE</param-name>
<param-value>false</param-value>
</context-param>
是true時(shí)不行,改為false可以了。
2、并且在BirtFilter中的init方法中,不能map.put("report");這個(gè)應(yīng)該是和Birt有沖突。

public void init(FilterConfig fc) throws ServletException
{

this.context = fc.getServletContext();

/**//*
* 這里注意,在項(xiàng)目目錄的命名時(shí),不要取和birt內(nèi)置的一些servlet名重復(fù)。 請(qǐng)根據(jù)項(xiàng)目的web.xml自行配置。
* (包括frameset、run、preview、download、parameter、document、output)
*/
map.put("frameset", "ViewerServlet");
map.put("preview", "EngineServlet");
//map.put("report", "ViewerServlet");
}
在提供的代碼及說(shuō)明中,盡量的全面,為初學(xué)者提供個(gè)參考吧!有不明白者或需要探討的地方或不足之處,歡迎聯(lián)系我!
-東營(yíng) sparta-紫杉 原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處 :)
http://m.tkk7.com/SpartaYew/
SpartaYew@163.com
QQ:22086526