在struts2应用中,关于Ireport子报表$F(XXX)作为数据源问题的解决方法
近来要给客户做子报表,采用之前的jasper+ireport来开发,结果发现子报表上数据源是个问题,查了很多技术文章,却被难在了newnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{addresses})上,发现很多人和我一样,无法解决这个报错问题,好象是只有在struts2应用中才出这个问题。由是搞了一整天,最终得到了一个解决方法,如下:
setJasperName("deomReport"); List slist1=new ArrayList(); OrderedMap srow = new LinkedMap(); srow.put("zip", "100000"); srow.put("address", "北京市**********"); srow.put("email", "[email protected]"); slist1.add(srow); List slist2=new ArrayList(); srow = new LinkedMap(); srow.put("zip", "310012"); srow.put("address", "杭州**********"); srow.put("email", "[email protected]"); slist2.add(srow); OrderedMap row = new LinkedMap(); row.put("name", "徐翔"); row.put("duty", "软件工程师"); row.put("age", 33); row.put("addressList", new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(slist1)); list.add(row); row = new LinkedMap(); row.put("name", "陈文平"); row.put("duty", "软件工程师"); row.put("age", 24); row.put("addressList", new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(slist2)); list.add(row); reportParameters.put("SUBREPORT_DIR", "E:\\\\workspace\\demo\\struts2.1.8.1\\WebRoot\\cn\\com\\jandar\\jasper\\demo\\"); reportParameters.put("TITLE", "子报表演示样例"); return "pdf";
<action name="subReport" class="cn.com.jandar.jasper.demo.JasperReportDemo" method="subReport"> <!-- 报表返回设定 --> <result name="pdf" type="jasper"> <param name="location"> /cn/com/jandar/jasper/demo/${jasperName}.jasper </param> <param name="imageServletUrl">/cn/com/jandar/jasper/demo/images/</param> <param name="dataSource">list</param> <param name="format">PDF</param> <param name="documentName">subReportPDF</param> <param name="reportParameters">reportParameters</param> </result> </action>
在主报表中添加参数addresslist,并且通过参数属性界面更改参数类型(默认为String类型的),更改为java.lang.Object
打开在主报表中添加的子报表控件的属性面板,查看connectiontype属性,选择Useadatasourceexpression选项
打开在主报表中添加的子报表控件的属性面板,查看DataSourceExpression属性,填写$F{addressList}这样,子报表中可以得到addressList这个数据源,整个关键在于
row.put("addressList",newnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource(slist2));
放进主报表数据源的时候要已经是JRBeanCollectionDataSource类型了。
好了,如果大家还有什么问题可以给我留言
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30