EG:打印文件结果打印出一片空白
原因:使用了null的数据源而不是JREmptyDataSource
以下为正确代码
public <T> List<JasperPrint> createJasperPrint_1(List<T> list,
Map<String, Object> imgMap, Map<String, Object> pathMap) {
List<JasperPrint> printList = new ArrayList<JasperPrint>();
if (pathMap != null) {
try {
for (String kn : pathMap.keySet()) {
ServletContext context = ServletActionContext
.getRequest().getSession().getServletContext();
String file = context.getRealPath(String.valueOf(pathMap.get(kn)));
JasperReport report = (JasperReport) JRLoader
.loadObject(file);
JRDataSource source = new JRBeanCollectionDataSource(
list);
JasperPrint print = new JasperPrint();
//如果数据集为空 则应该使用JREmptyDataSource 否则会打印空白页面
if (list == null) {
print = JasperFillManager.fillReport(
report, imgMap, new net.sf.jasperreports.engine.JREmptyDataSource());
} else {
print = JasperFillManager.fillReport(
report, imgMap, source);
}
if (print != null)
printList.add(print);
}
} catch (JRException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return printList;
}