sparta-紫杉 2010-8-18 8:12
開發環境: 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。該種方法已經試驗可行。
2、可下載相關部件,然后安裝到各相關目錄。該種方法沒有試過,但可參見網絡文章“Birt安裝指南”。
3、直接通過Eclipse的Software Updates安裝Birt插件,參見如下網址:
http://download.eclipse.org/birt/downloads/updmaninst2.2.php
筆者就是通過第3種方式安裝Birt到Eclipse的。
二、在已有項目中做報表,報表文件默認為test.rptdesign
1、將報表文件放到webapp/jsp/report/文件夾下。
當然,假設Birt報表文件嵌入在jsp中,Birt在找相關的*.rptdesign文件進行顯示時,會默認從webapp文件夾下找。
筆者將*.rptdesign文件放到webapp/jsp/report/文件夾下之后,birt就會找不到該文件,怎么辦?可以在Web.xml中設置:
<context-param>
<param-name>BIRT_VIEWER_WORKING_FOLDER</param-name>
<param-value>/jsp/report/</param-value>
</context-param>
2、編輯數據源, 數據源為“腳本數據源 scripted Data Source”,為該數據起個名字為SSH2。
3、編輯數據集,該數據集和SSH2數據源掛接。
1)、數據集的open屬性:
factory = new Packages.test.report.BirtFactory.getInstance();
unitsList = factory.getUnitsLst(params["unitsId"].value);
iterator = unitsList.iterator();

注意: 上述BirtFactory類在第四標題中給出代碼!
2)、數據集的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)、數據集的close屬性:
units = null;
iterator = null;
unitsList = null;
factory=null;
4、本例子為顯示相關的“單位列表”,因此報表文件顯示字段為: 單位id, 單位名稱, 單位類型。
5、還必須為報表文件設置參數:
1)、設置報表參數: 在“Report Parameters”中設置,本例中為unitsId,默認值為10011001。
2)、設置Data Set參數:
在“Data Set”點右鍵編輯,在彈出的窗口中再點“Parameters”,設置一個與報表參數同名的參數unitsId,默認值為10011001,要注意同名,要不然,在頁面上會找不到相關的參數而報錯。
三、將報表文件嵌入到jsp

<%
@ taglib uri="/birt.tld" prefix="birt" %>
<!-- 報表 -->
<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在通過URL輸出顯示報表時, 其中的frameset被Struts2過濾掉,須編寫下面的自定義過濾器,當然這個自定義過濾器不是我寫的,是一個叫做Shoru的人寫的:

public class BirtFilter implements Filter
{

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


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


/** *//**
* debug開關
*/
static boolean debug = false;


/** *//**
*/

public void destroy()
{
map = null;
}


/** *//**
* 過濾birt請求,轉發到對應的servlet,以繞過其他過濾器,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:這里的判斷只是簡單地調用contains方法,這樣就帶來較多限制。
* 比如工程子目錄的命名、struts命名空間等都受到birtViewer的約束。待改進。
*/

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

if (rd != null)
{

if (debug)
{
System.out.println(">>>Redirect successfully executed");
}
// 跳過其他過濾器,跳轉到對應的servlet
rd.forward(request, response);

} else
{

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

// 將請求交給下一個過濾器
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();

/**//*
* 這里注意,在項目目錄的命名時,不要取和birt內置的一些servlet名重復。 請根據項目的web.xml自行配置。
* (包括frameset、run、preview、download、parameter、document、output)
*/
map.put("frameset", "ViewerServlet");
map.put("preview", "EngineServlet");
map.put("report", "ViewerServlet");
}
}
并且需要在Web.xml中進行如下配置才生效(并且必須要配置在Struts2的過濾器的前面):
<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來管理Bean、事務及數據源:
編寫BirtFactory類:

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

}
}
編寫在BirtFactory中用到的InitServlet類,以便在web容器啟動時,獲得ApplicationContext.xml中相關上下文參數。

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中進行配置:
<!-- Birt和Spring集成的Servlet ,用以Birt可利用Spring完成報表數據源的建立、
數據的獲取及事務的管理功能!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項目
1、拷相關的文件:
首先從 http://download.eclipse.org/birt/downloads/build_list.php 下載Birt的運行時: birt-runtime-2_3_2_2.zip,然后解壓。
復制birt-runtime-2_3_2\WebViewerExample\下的logs,report,scriptlib,webcontent文件夾到avatar項目的webapp目錄下。
復制birt-runtime-2_3_2\WebViewerExample\WEB-INF\下的lib,platform,tlds文件夾,及相關server-config.wsdd,viewer.properties文件到avatar項目的webapp\WEB-INF下面。
將\birt-runtime-2_3_2\ReportEngine\文件夾打包成reportEngine.zip,復制到avatar\webapp\WEB-INF\lib文件夾下(這個東西缺了不行)。
2、web.xml配置,好家伙,這個配置比較繁瑣:
<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報表-試驗 ,在試驗后刪除。 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:

/**//*
* 試驗 Birt 報表,傳查詢參數
*/

public String getUnitsList()
{

if (null == unitsId || "".equals(unitsId))
{
unitsId = "10011001";
}
return SUCCESS;
}
5、業務邏輯Buz: 執行dao.getUnitsList(String unitsId)
6、Model:單位表的Pojo。
7、Dao: 通過執行getHibernateTemplage().getUnitsList(String unitsId)提取相應數據(當然了,你得確保數據庫里面有數據才可以)。
8、applicationContext.xml:
注入UnitsModel、注入業務邏輯組件、注入SessionFactory。
六、運行
在游覽器中運行Struts2的Action, 如下URL:
http://192.268.0.72:7001/webapp/UnitsQry2
若成功,就會顯示相關結果,如下截圖:

七、常見問題歸納
1、提示如下錯誤: 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時不行,改為false可以了。
2、并且在BirtFilter中的init方法中,不能map.put("report");這個應該是和Birt有沖突。

public void init(FilterConfig fc) throws ServletException
{

this.context = fc.getServletContext();

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