1
votes

I'm generating an HTML report for showing table data in a web application, using JasperReports and Spring MVC 3.

Does JasperReports provide some kind of pagination method? Or, is it possible to paginate data in the generated report using some Spring MVC utilities?

2

2 Answers

2
votes

Yes, the JasperReports provides some pagination methods.

You can set this exporters parameters:

JRHtmlExporterParameter.BETWEEN_PAGES_HTML
JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS

The BETWEEN_PAGES_HTML exporter parameter accepts a java.lang.String to replace the default page separator when exporting to HTML format.

When set to Boolean.TRUE, the IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS exporter parameter ensures that all empty rows on the resulting HTML table are collapsed.

When PROPERTY_ONE_PAGE_PER_SHEET is not set, all document pages will be printed out into a single sheet. Page breaks appear as supplementary empty rows between pages.

You can read this guide for more information and explore this example for Struts.

You can use the IS_IGNORE_PAGINATION built-in report parameter or isIgnorePagination report's template property.

You can set this parameters to avoid breaking documents into multiple pages.

You can use JasperReports API - JasperDesign.setIgnorePagination(boolean ignorePagination) method.


Additions:

The related post on SO: Is Jasper Reports the appropriate solution to display reports in a web application?

You can look at the sample report in $JASPERREPORTS_DIR$\demo\samples\nopagebreak folder (distributed with JasperReports pack).

1
votes

I've had a look at this example: http://helptodeveloper.blogspot.com/2010/02/jasper-reports-with-pagination.html and checked the webapp demo included in the JasperReports package (in particular the viewer.jsp).

I've tried to achieve something similar using Spring MVC.

In my controller class I've modified my request-handling method this way:

@RequestMapping("/report")
public ModelAndView showHtmlReport(HttpServletRequest request,
        HttpServletResponse response,
        @RequestParam(value = "page", required = false) String pageIndex,
        ModelMap model) {

    // do something...

    if (StringUtils.isNotBlank(pageIndex)
            && StringUtils.isNumeric(pageIndex)) {
        model.put("page_index", new Integer(pageIndex));
    }

    // do something else...

    return new ModelAndView("htmlReport", model);
}

Then I've extended the standard JasperReportsHtmlView provided by Spring, overriding the renderReport method:

@Override
protected void renderReport(JasperPrint populatedReport,
        Map<String, Object> model, HttpServletResponse response)
        throws Exception {

    // check something...

    if (model.containsKey("page_index")) {
        Map<net.sf.jasperreports.engine.JRExporterParameter, Object> expParams = 
                this.getConvertedExporterParameters();
        expParams.put(
                net.sf.jasperreports.engine.JRExporterParameter.PAGE_INDEX,
                model.get("page_index"));
    }

    // check something else...

    super.renderReport(populatedReport, model, response);
}

NOTE: I actually extended the html-view class for another reason ( Images not shown in JasperReports' html report ), but following the same approach I solved also this problem.

NOTE pt.2: I'm new to Spring Framework, so if my answer is not completely correct... Try to understand me! :)