gpt4 book ai didi

java - BIRT Report using POJOs unning under tomcat

转载 作者:行者123 更新时间:2023-11-28 22:29:02 24 4
gpt4 key购买 nike

这是我第一次使用 BIRT 引擎。我专注于使用 POJO 的问题。

我有简单的 POJO:

class Parent {
private String name;
public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}

简单的 BIRT 报告只打印 parent 姓名(我只打印 1 个 parent 的名字,这意味着 - 我只发送包含 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 运行时 Java 库有什么建议吗?

它还取决于,你在上面指定了哪个依赖项。

使用这个 POM.XML PDF 报告生成成功,所有 POJO 数据都正确绑定(bind):

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

最佳答案

我认为您的 POJO 在 Tomcat 中不起作用,因为您忘记为 BIRT 引擎提供类加载器。尝试添加这个:

import org.eclipse.birt.report.engine.api.EngineConstants;

在你的构造函数中:

config.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, BirtRenderer.class.getClassLoader());

关于java - BIRT Report using POJOs unning under tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29409381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com