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;
}