【问题标题】:BIRT Report using POJOs unning under tomcat使用在 tomcat 下运行的 POJO 的 BIRT 报告
【发布时间】: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 类的源代码。

标签: java tomcat birt


【解决方案1】:

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

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

在你的构造函数中:

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

【讨论】:

  • 很抱歉注意到你,但这没有任何作用。我尝试了许多不同的选项,还尝试在 executeReport 方法中将类加载器添加到上下文中。解决不了问题。
  • birt引擎的日志呢,有什么线索吗?
  • BIRT 日志文件在 WARNING 级别为空。
【解决方案2】:

好的,我发现了一个问题。

问题出在 Java 库上。 Apache FOP 使用 Apache Batik,它有一个工件 batik-js。这是一个瓶颈,因为 BIRT 运行时库使用更新的版本。

很难找到问题,因为 BIRT Runtime 使用“org.mozilla.javascript-1.7.2.jar”,而 FOP 使用“batik-js-1.7.jar”。它们都存储相同的 Java 类。

所以当我们指定对 TOP 的 FOP 依赖时,Java ClassLoader 采用了旧版本(FOP 版本)的 jar。

这仍然是一个问题——为什么 BIRT 运行时开发人员使用相同的 Java 类创建不同的 maven 工件。

所以我的新 pom.xml 是:

    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop</artifactId>
        <version>1.0</version>
        <exclusions>
            <exclusion>
                <artifactId>batik-js</artifactId>
                <groupId>org.apache.xmlgraphics</groupId>
            </exclusion>
        </exclusions>
    </dependency>

     <dependency>
        <groupId>org.eclipse.birt.runtime</groupId>
        <artifactId>org.eclipse.birt.runtime</artifactId>
        <version>4.4.2</version>
    </dependency>

现在一切正常!

【讨论】:

    猜你喜欢
    • 2017-03-08
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    相关资源
    最近更新 更多