java JasperReport异常打印
我正在用JasperReport制作报告引擎。到目前为止一切正常,但我有点不理解。在我的代码中,我试图编译模板文件并返回JasperReport对象,如果编译失败,则编译另一个文件并返回错误消息。但由于某些原因,它不起作用。这是我的代码:
/**
* Generates JasperPrint object from the Template file
* @param Template File Name (String)
* @param Parameters (Map<String, Object>)
* @param Collection of Value Objects (Collection, List, ArrayList)
* @return JasperPrint
*/
private JasperPrint getJRPrint(String tmpltFileLocation, Map<String, Object> params, JRBeanCollectionDataSource dataSource) {
JasperPrint jrPrint = null;
log.info("ReportEngine: compiling " + tmpltFileLocation);
try {
JasperReport jasperReport = JasperCompileManager.compileReport(tmpltFileLocation);
jrPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
} catch (JRException ex) {
ex.printStackTrace();
return getErrorJRPrint(ex);
}
return jrPrint;
}
private JasperPrint getErrorJRPrint(Exception ex) {
JasperPrint errJrPrint = null;
Map<String, Object> errParams = new HashMap<String, Object>();
errParams.put("errorMessage", ex.getMessage());
try {
JasperReport jasperReport = JasperCompileManager.compileReport(reportFolderName + "errReport.jrxml");
errJrPrint = JasperFillManager.fillReport(jasperReport, errParams);
} catch (Exception ex2) {
ex2.printStackTrace();
}
return errJrPrint;
}
错误模板文件在那里(我试图删除它,但它抱怨文件丢失,所以它可以看到它)。在我的模板文件中,我只是在打印错误消息,我还试图打印一些静态文本,但它不起作用。有什么问题吗
# 1 楼答案
我试过你的代码,没有问题
在您正在打印的报告中,如果您不填写报告查询或发送数据源,报告将始终为空或不打印(取决于您设置的“When No Data”(无数据时)变量)
如果这就是问题所在,那么设置“When no data”(无数据时)变量以打印除Detail(细节)之外的所有部分,并将消息放在任何其他波段
这就是变量:
如果使用iReport,则更容易更改它
注意:如果使用iReport创建报告,请确保iReport使用相同的JasperReport库版本