【发布时间】:2017-01-19 22:43:50
【问题描述】:
我用 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;
}
错误模板文件在那里(我试图删除它,它抱怨文件丢失,所以它可以看到它)。在我的模板文件中,我只是打印错误消息,并且我尝试打印一些静态文本,但它不起作用。可能是什么问题?
【问题讨论】:
-
您的问题解决了吗?
标签: java exception-handling jasper-reports