JASPER REPORT:子报表噩梦,数据源是列表

JASPER REPORT: Subreport nightmare, datasource is List

无法查看包含子报表的主报表...

主要JavaClass

   public class SubRepoExample {

    public static void main(String[] args) throws JRException {
       SubRepoExample repo = new SubRepoExample();
       repo.combineMasterAndSubreport();
    }

     public void combineMasterAndSubreport() throws JRException{

         JasperReport main = JasperCompileManager.compileReport("src/subrepoexample/masterReport.jrxml");

        JasperReport sub = JasperCompileManager.compileReport("src/subrepoexample/subReport.jrxml");

        //create a list for holding the subreport object



        SubreportWrapper subDataWrap = new SubreportWrapper();

        List<SubreportObject> subData = subDataWrap.getSubData();

        Map para = new HashMap();
        //pass the report itself through a parameter
        para.put("SUBREPORT", sub);
        //pass the list to parameter
        para.put("subData",subData);


        JasperPrint jp = JasperFillManager.fillReport(main, para,new   JRTableModelDataSource(createMasterData()));
        JasperViewer.viewReport(jp, false);  

    }

      public DefaultTableModel createMasterData(){

        String [] cols = {"COL_1","COL_2","COL_3"};
        Object[][] data = {{"Data","Data","Data"},
                        {"Data","Data","Data"},                                  
                      };
         DefaultTableModel dtm = new DefaultTableModel(data,cols);

    return dtm;}

}

SubreportObject class 负责创建最终放置在列表中的子报表数据。

 public class SubreportObject {
      String name;
      int age;

     public SubreportObject(String name,int age){
         this.name = name;
         this.age = age;  
         }

     public String getName() {
     return name;
         }

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

     public int getAge() {
     return age;
         }

     public void setAge(int age) {
     this.age = age;
         }

      }

豆厂Class

 public class SubreportWrapper {
    //String name;
    List<SubreportObject> subData;

//    public SubreportWrapper(List<SubreportObject> subData){
//        this.subData = subData;
//    }

    public List<SubreportObject> getSubData() {

        subData = new ArrayList();
        subData.add(new SubreportObject("Jane",20));
        subData.add(new SubreportObject("Mike",25));
        subData.add(new SubreportObject("Kev",20));
        return subData;
    }
    public void setSubData(List<SubreportObject> subData) {
        this.subData = subData;
    }  
  }

Main jrxml 和 Subreport jrxml 中的 JRXML 片段

通过 JRBeanCollection 的数据源片段 SUBREPORT_DIR就位

错误堆栈跟踪

我做错了什么?

您正在设置 JRBeanCollectionDatasource 构造函数并将子数据作为字段 $F{subData},请改用参数:$P{subData}.

无需在 SUBREPORT 参数中传递子报表,因为 .jasper 文件将根据 subreportExpressionSUBREPORT_DIR 参数加载。