我尝试将JasperReport与SpringMVC集成,但是在打印jdbc报告时它不起作用

时间:2018-01-10 01:44:49

标签: java spring spring-mvc jasper-reports

这是我的视图解析器spring xml:

<bean id="jasperReportsViewResolver" class="org.springframework.web.servlet.view.jasperreports.JasperReportsViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView"></property>
    <property name="prefix" value="/WEB-INF/jasper/"></property>
    <property name="suffix" value=".jrxml"></property>
    <property name="contentType" value="text/html"></property>
    <property name="jdbcDataSource" ref="druidDataSource"></property>
    <property name="reportDataKey" value="jrDatasource"></property>
    <property name="order" value="2"></property>
</bean>

这是我的控制器:

@RequestMapping(value = "report/jdbc2", method = RequestMethod.GET)
public String jdbcReport2(
        @RequestParam(value = "syudylsh", required = true) String syudylsh,
       Model model
) throws JRException, SQLException, IOException, ClassNotFoundException {
    model.addAttribute("empNo",7369);
    model.addAttribute("format","pdf");
    model.addAttribute("jrDatasource", new JREmptyDataSource());
    return "jdbc-report";
}

正确的结果是:

The Normal Result Show Like this

正确的结果代码:

@Test
public void testPrintPdf() throws JRException, InstantiationException, IllegalAccessException {
    DBTools.initDataSource("datasources/oracle-scott.properties",DruidDataSource.class);
    Connection connection = DBTools.getConnection();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("empNo", 7369);
    InputStream resourceAsStream = DBTools.class.getClassLoader().getResourceAsStream("jasperreport/demo/jdbc-report.jasper");
    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(resourceAsStream);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);
    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("d:/ireport_out/jdbc-report.pdf"));
    exporter.exportReport();
}

现在获取Bug结果是:

The Right Code Show

我在视图解析器中配置了这个,但它似乎不起作用。谁可以帮助我?,帮助....

Spring xml Has config

1 个答案:

答案 0 :(得分:0)

现在问题已经解决了。

我已经改变了

model.addAttribute("jrDatasource", new JREmptyDataSource());

//datasource is db source
model.addAttribute("jrDatasource", datasource);