views:

470

answers:

2

I making report engine with JasperReport. Everything works fine so far but I have small miss understanding. In my code I'm trying to compile template file and return JasperReport object and if the compilation is failed then compile another file and return error message. But it does not work for some reason. Here is my code:

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

The error Template file is there (I tried to delete it and it complains that the file is missing, so it can see it). In my template file I'm just printing error message and also I tried to print some static text but it does not work. What could be the problem?

A: 

Why do you need to compile the jrxml at runtime? Have you tried compiling it in iReport and getting the JasperReport object via
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);

Anyway, if the jrxml file is generated, try compiling it in iReport and see if it has any complaints.

Bozho
+1  A: 

I have tried your code and nothing wrong with it.

In the report you are printing, if you dont fill the report query or send a datasource, the report will always be empty or not printed (depending on what you did you set the "When No Data" variable)

If thats the problem, then set the "When no data" variable to Print all sections except Detail and put your message in any other band.

This is the variable:

  jasperReport.WHEN_NO_DATA_TYPE_BLANK_PAGE
  jasperReport.WHEN_NO_DATA_TYPE_NO_PAGES
  jasperReport .WHEN_NO_DATA_TYPE_ALL_SECTIONS_NO_DETAIL

Its easier to change it if you are using iReport.

Note: If you are creating the report with iReport, make sure that iReport uses the same JasperReport libraries versions.

medopal