java 如何将 JRBeanCollectionDataSource 传递给 iReport?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13456114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:48:28  来源:igfitidea点击:

How To Pass a JRBeanCollectionDataSource to iReport?

javajasper-reportsjavabeansdatasource

提问by qwer1234

I'm currently trying to use jasper to help me create reports. I have the information and data that I want displayed in this method:

我目前正在尝试使用 jasper 来帮助我创建报告。我有我想在这个方法中显示的信息和数据:

private void writeToFile(final List<ScenarioLoadModel> sceneLoadModel) throws Exception {
  final BufferedWriter bw = new BufferedWriter(new FileWriter("/Uma/nft/result.psv"));

  for (final ScenarioLoadModel slm : sceneLoadModel) {
    bw.write(slm.getScenarioId() + PSP + slm.getScenarioId() + PSP + slm.getScenarioConfig().getName() + PSP + slm.getLoad() + PSP + "" + EOL);
    if (!slm.getScenarios().isEmpty()) {
      final int tempCount = slm.getScenarios().get(0).getTemplates().size();
      final int sceneCount = slm.getScenarios().size();
      for (int tempIdx = 0; tempIdx < tempCount; tempIdx++) {
        String id = null;
        int pass = 0;
        int fail = 0;
        final Map<String, BigDecimal> metricMap = new HashMap<String, BigDecimal>();
        final DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();
        for (int sceneIdx = 0; sceneIdx < sceneCount; sceneIdx++) {
          final Template temp = slm.getScenarios().get(sceneIdx).getTemplates().get(tempIdx);
          if (temp.isError()) {
            fail++;
          } else {
            pass++;
          }
          if (sceneIdx == 0) {
            id = temp.getId();
          }
          final MetricGroupModel mgm = slm.getScenarios().get(sceneIdx).getMetricGroupModel().get(tempIdx);
          if (mgm != null) {
            for (final MetricModel mm : mgm.getMetricModel()) {
              for (final MetricValue mv : mm.getMetricValue()) {
                dataset.add(mv.getValue(), new BigDecimal(0.0), mv.getType(), id);
              }
            }
          }
        }
        final TemplateConfig tc = TemplateManager.getTemplateConfig(id);

        bw.write(slm.getScenarioId() + PSP);
        bw.write(id + PSP + tc.getName() + PSP + 1 + PSP + pass + "/" + fail);
        for (final Object row : dataset.getRowKeys()) {
          final Number mean = dataset.getValue((String) row, id);
          bw.write(PSP + row + PSP + mean);
        }
        bw.write(EOL);
      }
    }
  }

  bw.close();
}

From my understanding I create Beans and then put them all in a Bean Factory, to create my object that will be ready to be passed to iReport.

根据我的理解,我创建了 Bean,然后将它们全部放入 Bean 工厂中,以创建准备传递给 iReport 的对象。

How can I put all this information into a Bean? I essentially want the bean to include the scenario/test case and whether or not it passed. (This is for test automation)

如何将所有这些信息放入 Bean 中?我基本上希望 bean 包含场景/测试用例以及它是否通过。(这是用于测试自动化)

回答by Jacob Schoen

I tried to read your code to make a a best guess at what columns you would want, but with no context, I have no clue. All the bean is a pojo, with private fields and public getters and setters.

我试图阅读您的代码以对您想要的列做出最佳猜测,但没有上下文,我不知道。所有 bean 都是一个 pojo,具有私有字段和公共 getter 和 setter。

Assuming there is no grouping and essentially each ScenarioLoadModelwill correspond to one row in the report you would end up with a bean like this:

假设没有分组,并且基本上每个分组ScenarioLoadModel都对应于报告中的一行,您最终会得到一个像这样的 bean:

public class ScenariaResults {

    private String id;

    private String name;

    private String load;

    private int passCount;

    private int failCount;

    public ScenariaResults(String id, String name, String load, int passCount,
            int failCount) {
        super();
        this.id = id;
        this.name = name;
        this.load = load;
        this.passCount = passCount;
        this.failCount = failCount;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getLoad() {
        return load;
    }

    public void setLoad(String load) {
        this.load = load;
    }

    public int getPassCount() {
        return passCount;
    }

    public void setPassCount(int passCount) {
        this.passCount = passCount;
    }

    public int getFailCount() {
        return failCount;
    }

    public void setFailCount(int failCount) {
        this.failCount = failCount;
    }

    @Override
    public String toString() {
        return "ScenariaResults [id=" + id + ", name=" + name + ", load="
                + load + ", passCount=" + passCount + ", failCount="
                + failCount + "]";
    }

}

So basically in the code you have above you build instances of ScenarioResultsand add them to a list. Once you have the list, all you need to do is create a JRDataSource:

所以基本上在你上面的代码中,你构建实例ScenarioResults并将它们添加到列表中。获得列表后,您需要做的就是创建一个 JRDataSource:

List<ScenarioResults> dataBeanList = ...call your method to get the list of results
//create the datasource
JRDataSource dataSource = new JRBeanCollectionDataSource(dataBeanList);

Now when designing the report in iReport it can be a little tricky to get the fields imported automatically. Basically first add your project with the bean to the classpath in iReports (could just point it to the bin folder or jar file`): Tools -> options -> classpath tab. Now follow these steps to add the fields.

现在在 iReport 中设计报告时,自动导入字段可能有点棘手。基本上首先将带有 bean 的项目添加到 iReports 中的类路径(可以将其指向 bin 文件夹或 jar 文件`):工具 -> 选项 -> 类路径选项卡。现在按照以下步骤添加字段。

  1. Click the following icon: Create datasource
  2. Select the JavaBean Datasourcetab.
  3. Enter the classname of your bean. (ex. ScenarioResults)
  4. Click Read attributes
  5. Highlight the fields you want in the report and click Add Selected Field(s).
  6. Click OK.
  1. 单击以下图标: 创建数据源
  2. 选择JavaBean Datasource选项卡。
  3. 输入 bean 的类名。(例如ScenarioResults
  4. 点击 Read attributes
  5. 突出显示报告中所需的字段,然后单击Add Selected Field(s)
  6. 单击OK

Now if you want to test what the report looks like with data, and not just an empty datasource, this is where the Factory comes in. It is only for testing while using iReport.You need to create a class that will essentially create a dummy data set for you. It should look something like:

现在,如果您想用数据测试报告的外观,而不仅仅是空数据源,这就是 Factory 的用武之地。它仅用于在使用 iReport 时进行测试。您需要创建一个类,该类实质上将为您创建一个虚拟数据集。它应该看起来像:

import java.util.ArrayList;
import java.util.List;

public class ScenarioResultsFactory {

    public static List<ScenarioResults> createBeanCollection() {
        List<ScenarioResults> list = new ArrayList<ScenarioResults>();       
        list.add(new ScenarioResults("1", "test", "load", 10, 5));
        //add as many as you want       
        return list;
    }

}

Now you need to create a Datasource pointing to it in iReport.

现在您需要在 iReport 中创建一个指向它的数据源。

  1. Next to the Datasource dropdown in the toolbar click the icon with the tooltip `Report Datasources.
  2. Click New.
  3. Select JavaBeans set datasource. Click Next.
  4. For name enter ScenarioResultsFactory.
  5. For the Factory class you need to put the classname including package. So if the class is in the compackage you should have com.ScenarioResultsFactoryhere.
  6. For the static method put createBeanCollectionif not already there.
  7. Check the Use field descriptioncheck box. Click Testto make sure it worked.
  8. Click Save.
  1. 在工具栏中的数据源下拉菜单旁边,单击带有工具提示“报告数据源”的图标。
  2. 单击New
  3. 选择JavaBeans set datasource。单击Next
  4. 对于名称输入ScenarioResultsFactory
  5. 对于工厂类,您需要放置类名,包括包。所以如果类在com包中,你应该在com.ScenarioResultsFactory这里。
  6. 对于静态方法 put createBeanCollectionif not already there。
  7. 选中Use field description复选框。单击Test以确保它有效。
  8. 单击Save