技术开发 频道

使用JasperReport与iBATIS开发Web报表



四、        iBATIS数据填入JasperReport
就通常而言,采用Java Bean作为iBATIS的返回对象,相比起java.util.Map对象来说,更加的方便与可行。很多的开发人员采用iBATIS的这种方式来进行数据的映射,同时,此方法还可以无缝的将iBATIS与JapserReport集成起来。
在JasperReport中,提供了一个JRDataSource的实现,从而开发人员可以通过此类来传递iBATIS的list对象给JasperReport模板。而JRBeanCollectionDataSource类使用JavaBean来构造,从而可以通过循环查找collection并获得相应的bean属性。如下的代码示例说明了如何在调用JasperReport引擎时实例化JRBeanCollectionDataSource对象。

import java.io.File; import java.util.HashMap; import java.util.List; import net.sf.jasperreports.engine.JRRuntimeException; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.util.JRLoader; public class SearchBean { private final static String JAVA_BEAN_REPORT = "monthly_sales_java_beans.jasper"; private final static String LIST_OF_MAP_REPORT = "monthly_sales_list_of_maps.jasper"; public String generateFromJavaBeans () { try { ServiceLocatorIF sl = (ServiceLocatorIF) FacesUtils .getManagedBean("serviceLocatorBean"); List list = sl.getDao().getSqlMapClient().queryForList("salesByJavaBeansSQL", month); FacesUtils.setSessionAttribute("JASPER_PRINT", generateReport (list, JAVA_BEAN_REPORT)); viewReport = "true"; } catch (Exception ex) { ex.printStackTrace(); } return null; } public String generateFromListOfMaps () { try { ServiceLocatorIF sl = (ServiceLocatorIF) FacesUtils .getManagedBean("serviceLocatorBean"); List list = sl.getDao().getSqlMapClient().queryForList("salesByListOfMapsSQL", month); FacesUtils.setSessionAttribute("JASPER_PRINT", generateReport (list, LIST_OF_MAP_REPORT)); viewReport = "true"; } catch (Exception ex) { ex.printStackTrace(); } return null; } private JasperPrint generateReport (List dataList, String reportName) { JasperPrint jasperPrint = null; try { String localPath = FacesUtils.getServletContext().getRealPath("/"); File reportFile = new File(localPath + "WEB-INF" + File.separator + reportName); if (!reportFile.exists()) throw new JRRuntimeException(".jasper file not found. The report design must be compiled first."); JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath()); if (reportName.equals(JAVA_BEAN_REPORT)) { jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new JRBeanCollectionDataSource (dataList)); } else { jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new CustomJRDS (dataList)); } } catch (Exception ex) { ex.printStackTrace(); } return jasperPrint; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getViewReport() { return viewReport; } public void setViewReport(String viewReport) { this.viewReport = viewReport; } private String month = null; private String viewReport = null; }
在上面的代码中,定义的参数map,是在运行时传递相关的参数值给JasperReport。例如,可以在报表模板中定义一个名为REPORT_TITLE的参数,然后在运行时传递这一参数的值给它,传递的方式一般是健/值对的形式。例如Key=REPORT_TITLE,Value=Sale Report。当然,参数是传递给fillReport方法。然后,JasperReport会加载已经编译好的Jasper模板文件(.jasper)。最后调用静态的fillReport方法。
而JasperPrint对象是在数据展示或显示时需要用到的。而在本例中,采用了JRPdfExporter来作为输出的格式,即输出为PDF格式文件,请参考PdfServlet.java文件,代码如下所示:
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRPdfExporter; public class PdfServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { JasperPrint jasperPrint = (JasperPrint) request.getSession() .getAttribute("JASPER_PRINT"); List jasperPrintList = new ArrayList(); jasperPrintList.add(jasperPrint); JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList); ByteArrayOutputStream baos = new ByteArrayOutputStream(); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); try { exporter.exportReport(); } catch (JRException e) { throw new ServletException(e); } byte[] bytes = baos.toByteArray(); if (bytes != null && bytes.length > 0) { response.setContentType("application/pdf"); response.setContentLength(bytes.length); ServletOutputStream ouputStream = response.getOutputStream(); try { ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); } finally { if (ouputStream != null) { try { ouputStream.close(); } catch (IOException ex) { } } } } } }
尽管上面的JasperReport机制可以将iBATIS连接起来,但应该根据项目报表的需要对JavaBean进行修改与调整。而JasperReport字段对象可以很好的与普通的JDBC字段进行匹配。例如,JasperReport将Oracle的numeric字段类型对应的转成java.math.BigDecimal对象类型。而在iBATIS的Bean属性应该与JasperReport中定义的字段类型进行很好的匹配。需要对字段的类型进行认真仔细的选择,因为不同类型或是不同表达式对数据的展示有不同的效果。例如,BigDecimal类型比String类型更加适合货币格式。
0
相关文章