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://www.blogjava.net/SpartaYew/
SpartaYew@163.com
QQ:22086526