首页 > 解决方案 > 如何在 HTML 页面中显示 Jasper 报告

问题描述

所以,我已经制作了报告,但问题是我正在使用 GetMapping 来显示,它单独在另一个页面上打开,但我需要它在 div、表格、卡片甚至模式上打开. 它只是不能从上一页改变。

我不知道如何在不重定向页面的情况下打开它。任何建议表示赞赏

控制器上的方法

@GetMapping("/seguro")
    public void export(HttpServletResponse response) throws IOException, JRException, SQLException {
        response.setContentType("text/html");
        JasperPrint jasperPrint = null;
        jasperPrint = seguroReportService.generatePromissoria(1L);
        HtmlExporter htmlExporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());
        htmlExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        htmlExporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
        htmlExporter.exportReport();
    }

获取报告文件的方法

public JasperPrint generatePromissoria(Long id) throws SQLException, JRException, IOException {
        Connection conn = jdbcTemplate.getDataSource().getConnection();

        String path = resourceLoader.getResource("classpath:/reports/SeguroReport.jrxml").getURI().getPath();

        JasperReport jasperReport = JasperCompileManager.compileReport(path);
        // Parameters for report
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("titulo", "Relatório de Seguros");
        parameters.put("ID", id);

        JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conn);

        return print;
    }

应该打开报告的页面

报告在另一个页面中打开

标签: javahtmlspringjasper-reportsthymeleaf

解决方案


我使用 iframe 解决了这个问题,但是我没有在 src="" 上传递 HTML,而是在我的表单中针对 iframe 的名称在按钮上创建了一个 onClick 函数。

<iframe name="my_iframe" src=""></iframe>

<button onclick="this.form.target='my_iframe'">My Button</button>

推荐阅读