简体   繁体   English

(JasperReports) 使用JRBeanCollection作为DataSource,并在jrxml中处理

[英](JasperReports) Using JRBeanCollection as DataSource, and handle it in the jrxml

So, I obtain a list of DAO from my database, and then I pass a JRBeanCollectionDataSource to the fill method.所以,我从我的数据库中获取了一个 DAO 列表,然后我将一个 JRBeanCollectionDataSource 传递给了 fill 方法。 This works (makes an empty PDF).这有效(制作一个空的PDF)。

public void reportTest() {
    AwardDAO awardDAO = new AwardDAOImpl();
    List<Award> awards = null;

    try{

        awards = awardDAO.getAllAwards();
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(awards);

        JasperReport jasperReport =
        JasperCompileManager.compileReport("src/main/resources/hellojasper.jrxml");

        Map parameters = new HashMap();
        parameters.put("title", "Award Report");

        JasperPrint jasperPrint =
                JasperFillManager.fillReport(jasperReport, parameters, ds);
        JasperExportManager.exportReportToPdfFile(
                jasperPrint, "Awards.pdf");

       }
    catch (Exception e){
        e.printStackTrace();
    }
}

I don t know how to handle the info in the .jrxml file .我不知道如何处理 .jrxml 文件中的信息 I have searched for a while but I only got more confused.我已经搜索了一段时间,但我只是变得更加困惑。 I want to display a simple table.我想显示一个简单的表格。 Thanks!谢谢!

You must declare fields in jrxml corresponding getters in Award and use this fields in textField expressions of report.你必须在jrxml 中在Award 中对应的getter 中声明字段,并在report 的textField 表达式中使用这个字段。 That's all.就这样。
See example查看示例

stackoverflow.com/questions/21432871/accessing-the-only-java-bean-passed-to-jasper-records stackoverflow.com/questions/21432871/accessing-the-only-java-bean-passed-to-jasper-records

PS Are you sure that reportTest. PS你确定reportTest。 class know about path " src /main/resources/hellojasper.jrxml" at your application?知道您的应用程序中的路径“ src/ main/resources/hellojasper.jrxml”?

Make sure your Award class has getters and setters for each field you want to display in the PDF file.确保您的 Award 类对要在 PDF 文件中显示的每个字段都有 getter 和 setter。

For example if I want to display a Product class with the following fields:例如,如果我想显示具有以下字段的 Product 类:

<field name="productName" class="java.lang.String">
<field name="quantity" class="java.lang.String">
<field name="unitPrice" class="java.lang.String">
<field name="total" class="java.lang.String">

I can use a POJO to represent the Product class:我可以使用 POJO 来表示 Product 类:

public class Product {
    private String productName;
    private String quantity;
    private String unitPrice;
    private String total;

    public void setProductName(String productName){
        this.productName = productName;
    }

    public String getProductName(){return productName;}

    public void setQuantity(String quantity){
        this.quantity = quantity;
    }

    public String getQuantity(){return quantity;}

    public void setUnitPrice(String unitPrice){
        this.unitPrice = unitPrice;
    }

    public String getUnitPrice(){return unitPrice;}

    public void setTotal(String total){
        this.total = total;
    }

    public String getTotal(){return total;}
}

And then I can create a JRBeanCollectionDataSource:然后我可以创建一个 JRBeanCollectionDataSource:

List<Product> products = new ArrayList<Product>();
// Code to fill products
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(products);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM