最近些日子一直在研究报表生成的模块,用的是jasperreport,用ireport来设计报表模板,涉及到后台像子报表传递参数。 java
后台采用的是springmvc+spring+spring data jpa, spring
在后台向子报表传递map参数时的步骤是: express
1.在Parameters添加一个同后台传递到子报表map同名的parameter mvc
2.设置subMap的属性 spa
3.设置子报表的属性 设计
Parameters Map Expression属性填写$P{subMap}, code
(tips:此项只能用来向子报表传递普通的参数,好比string类型参数subparam) ip
Map<Object, Object> subMap = new HashMap<Object, Object>(); subMap.put("subparam", "subparam display");
Connection type属性选择Use a datasource expression 开发
(tips:此项向子报表传递数据集,即List类型等可迭代的集合类型参数) get
而后再Data Source Expression中填写
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(((Map<String,List>)$P{subMap}) .get("persons1"))
个人后台代码是
Map<Object, Object> mainMap = new HashMap<Object, Object>(); List<Persons> persons1 = (new PersonsFactory()).getPersons(); subMap.put("persons1", persons1); mainMap.put("subMap", subMap);
其中困扰了我好久的一点就是,按照高洪岩写的《Jasperreports + ireport报表开发详解》书中data source expression属性填写的格式为
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{subMap}.get("persons1"))
如此填写的后果实jasper文件编译没法经过,会报错
说object类型没有get()方法,并且Parameters参数类型中没有map类型,
只能选择父类型object,因此我把Object参数$P{subMap}强制转换为Map<String,List>,再去获取其中的List,
((Map<String,List>)$P{subMap}).get("persons1")
编译不报错,成功编译完成,
子报表map中List<Persons>也能够正常传递显示。