【发布时间】:2015-04-02 09:23:30
【问题描述】:
这是我第一次使用 BIRT 引擎。我专注于使用 POJO 的问题。
我有简单的 POJO:
class Parent {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
以及仅打印父级名称的简单 BIRT 报告(我仅打印 1 个父级的名称,这意味着 - 我发送的列表仅包含 1 条记录)。
我使用 JUnit 测试成功生成了带有父名称的 PDF,而当我尝试在 Tomcat 中生成它时 - BIRT 不会从 POJO 插入值。
您是否曾经关注过这个问题?
Birt 包装类:
public class BirtRenderer implements IBirtRenderer {
private EngineConfig config;
private IReportEngine engine;
private IReportEngineFactory factory;
public BirtRenderer() throws BirtException {
config = new EngineConfig();
config.setLogConfig("J:\\BirtLogs", Level.WARNING);
// config.setLogger(new Log4jHandler());
Platform.startup(config);
factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
}
public void destroy() {
engine.destroy();
Platform.shutdown();
}
public ByteArrayOutputStream executeReport(String reportName, String fileFormat, @SuppressWarnings("rawtypes") Map context) throws EngineException {
return executeReport(reportName, fileFormat, context, Locale.US);
}
public ByteArrayOutputStream executeReport(String reportName, String fileFormat, @SuppressWarnings("rawtypes") Map context, Locale locale) throws EngineException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
reportName = reportName + ".rptdesign";
/* for testing */
context = new HashMap();
ArrayList list = new ArrayList();
list.add(new TitleCustomerDao().next());
context.put("APP_CONTEXT_KEY_TITLE_CUSTOMER_DATA", list);
/* for testing end */
InputStream reportStream = this.getClass().getClassLoader().getResourceAsStream(reportName);
if (reportStream == null)
throw new EngineException(new BirtException("File '" + reportName + "' was not found."));
// Open the report design
IReportRunnable design = null;
// design =
// engine.openReportDesign("J:/birt-workspace/test/hello_world.rptdesign");
design = engine.openReportDesign(reportStream);
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
// task.setParameterValue("Top Count", (new Integer(5)));
// task.validateParameters();
RenderOption renderOption = getRenderOption(fileFormat);
renderOption.setOutputStream(outputStream);
task.setRenderOption(renderOption);
// task.setParameterValue("APP_CONTEXT_KEY_PARENTS", data);
task.setAppContext(context);
if (locale != null)
task.setLocale(locale);
if (!task.validateParameters()) {
throw new IllegalArgumentException("Parameters do not validate");
}
task.run();
task.close();
return outputStream;
}
private RenderOption getRenderOption(String fileFormat) {
if (fileFormat.equalsIgnoreCase("pdf")) {
PDFRenderOption PDF_OPTIONS = new PDFRenderOption();
// PDF_OPTIONS.setOutputFileName("J:/birt-workspace/test/hello_world.pdf");
PDF_OPTIONS.setOutputFormat("pdf");
return PDF_OPTIONS;
} else if (fileFormat.equalsIgnoreCase("html")) {
HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();
// HTML_OPTIONS.setOutputFileName("J:/birt-workspace/test/hello_world.html");
HTML_OPTIONS.setOutputFormat("html");
return HTML_OPTIONS;
} else
return null;
}
}
经过长时间的调查,我发现问题出在哪里。
问题是我在使用 BIRT 的同一个 Web 项目中使用 Apache FOP 库。 如果我们详细介绍一下 - 问题出在 Apache Batik 库上,Apache FOP 1.0(使用 >=1.7 Batik)和 BIRT 4.4.2(使用 1.6)使用该库。
这真的很奇怪,为什么里面的 BIRT Runtime Java 库会覆盖 Batik groupIds 和 artifacts。
您对如何在同一个 Web 项目中使用 Apache FOP 和 BIRT 4.4.2 Runtime Java 库有什么建议吗?
这还取决于您在顶部指定了哪个依赖项。
使用此POM.XML PDF报告生成成功,所有POJO数据绑定正确:
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.birt.runtime</artifactId>
<version>4.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
使用此 POM.XML PDF 报告以空 POJO 数据字段呈现:
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.birt.runtime</artifactId>
<version>4.4.2</version>
</dependency>
【问题讨论】:
-
很可能是classpath的问题,你把包含这个pojo的jar放在Tomcat的什么地方了?运行报告的代码在哪里:它是在与 birt 引擎相同的 webapp 中还是在单独的 web 应用程序中?
-
这个 POJO 和 BIRT 引擎包装 jar(用于创建引擎和配置它)进入 WEB-INF\lib forlder。数据来自控制器,放在WEB-INF\classes中。
-
你能展示创建和配置 BIRT 引擎的代码吗?我想我知道为什么它不起作用,但我想确定
-
Dominique,我刚刚添加了 BIRT Renderer 类的源代码。