【问题标题】:Set multiple collections of beans using a Java Beans Datasource使用 Java Beans 数据源设置多个 bean 集合
【发布时间】:2014-08-14 15:52:38
【问题描述】:

我需要在同一个 JasperReports 的报告中显示两个图表,一个显示产品数据,另一个显示人员数据。

通常,我可以定义一个返回单个 bean 集合的工厂类,例如产品数据 bean。我可以向报告提供两个 bean 集合吗?

我不能使用子报表,我需要使用 Java Beans 数据源。

【问题讨论】:

    标签: java jasper-reports datasource javabeans


    【解决方案1】:

    嗯,我不知道你是否还需要这个答案,但也许其他人会需要。

    您实际上并不需要两个 bean 集合,而是通过创建一个 helperClass 将其组合为一个,比如说 Products_Person_Class 然后添加您需要的字段的实例变量(不要忘记 getter 和 setter)然后设置变量对于另一个类中的实例;

    例如,假设您的报告中需要两个字段。一个来自个人数据,另一个来自产品数据(字段为 PersonId 和 ProductId)

    你让你的助手类

    public class Products_Person{
        private String personId;
        private String productId; 
    
    
      //make sure you have a constructor
    
      public Products_Person(){
    
      }
    
      public String getPersonId(){
        return personId;
       }
    
      public String setPersonId(String personId){
       this.personId = personId; 
    
      //do the same for productId
    
      }
    

    然后在另一个类中设置你的变量:

     //lets use hard code numbers for simplicity
      Products_Person p = new Products_Person();
      p.setPersonId("011111");
      p.setProductId("5555");
    

    然后将其添加到 Products_Person 类型的列表中

    List<Products_Person> list = new ArrayList<Product_Person>();
    
    list.add(p.getPersonId());
    list.add(p.getProductId());
    

    因此,在您的收藏中,您将拥有:

    public Collection<Products_Person> getBeanDataSource(){
       return list;
    

    然后在您的报告中您将能够看到这些字段,或者可能将数据放入图表中

       $F(personId)
       $F(productId)
    

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 2019-02-04
      • 2019-09-28
      • 1970-01-01
      • 2015-09-22
      • 2016-03-25
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      相关资源
      最近更新 更多