问题
Cannot view main report with subreport...
Main Java Class
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 reponsible for creating subreport data which eventually is placed on a List.
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;
}
}
Bean Factory 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;
}
}
JRXML snippets both in Main jrxml and Subreport jrxml
Datasorce snippet through JRBeanCollection The SUBREPORT_DIR is in place
Error stacktrace
What am I doing wrong?
回答1:
You are setting JRBeanCollectionDatasource
constructor with subdata as field $F{subData}
, use parameter instead: $P{subData}
.
No need to pass subreport in SUBREPORT
parameter since the .jasper file will be loaded based on subreportExpression
and SUBREPORT_DIR
parameter.
来源:https://stackoverflow.com/questions/59103670/jasper-report-subreport-nightmare-datasource-is-list