前言
上一篇我们使用了JRMapArrayDataSource数据源来与数据库进行交互,今天我们来介绍下如何使用实体对象作为数据源。
正题
跟之前的一样,我们要生成报表需要以下几个步骤:
1.引入jar包,请看《静态文本报表》 。
2.新建报表模版:
跟我们前一篇的AircraftReport报表模版是一样的,请参见《不同数据源之Map数据源》;
3.编译报表模版,请看《静态文本报表》。
4.编写实体对象:
作为数据源的实体对象需要注意:
1)不能有公共的属性。
2)必须有一个无参数的构造方法。
3)所有的属性必须要有getter和stter方法。
我们新建一个AircraftData(因为报表模版中的字段是大写的,所以这里面的字段也定义的大写的):
package com.dan.jasper; public class AircraftData { public AircraftData(String id,String name,String sex){ setID(id); setNAME(name); setSEX(sex); } public AircraftData(){ } private String ID; private String NAME; private String SEX; public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getNAME() { return NAME; } public void setNAME(String nAME) { NAME = nAME; } public String getSEX() { return SEX; } public void setSEX(String sEX) { SEX = sEX; } }
5.编写servlet:
由于实体对象数据源分为两种JRBeanArrayDataSource和JRBeanCollectionDataSource,下面将这两种形式都写出来:
1)JRBeanArrayDataSource:
package com.dan.servlet; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; 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.JRDataSource; import net.sf.jasperreports.engine.JasperRunManager; import net.sf.jasperreports.engine.data.JRBeanArrayDataSource; import com.dan.jasper.AircraftData; /** * 使用javabean作为数据源 * @author zdd * */ public class BeanArrayDSReportServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 2174316885912562306L; private JRDataSource createReportDataSource(){ JRBeanArrayDataSource dataSource; AircraftData[] resportRows = initializeMapArray(); dataSource = new JRBeanArrayDataSource(resportRows); return dataSource; } private AircraftData[] initializeMapArray(){ AircraftData[] reportRows = new AircraftData[4]; reportRows[0] = new AircraftData("n0", "zddn0", "female"); reportRows[1] = new AircraftData("n1", "zddn1", "female"); reportRows[2] = new AircraftData("n2", "zddn2", "female"); reportRows[3] = new AircraftData("n3", "zddn3", "female"); return reportRows; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletOutputStream servletOutputStream = resp.getOutputStream(); InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/WEB-INF/classes/reports/AircraftReport.jasper"); try{ JRDataSource dataSource = createReportDataSource(); JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream,new HashMap(),dataSource ); resp.setContentType("application/pdf"); servletOutputStream.flush(); servletOutputStream.close(); }catch(Exception e){ StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); resp.setContentType("text/plain"); resp.getOutputStream().print(stringWriter.toString()); } } }
2)JRBeanCollectionDataSource:
package com.dan.servlet; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; 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.JRDataSource; import net.sf.jasperreports.engine.JasperRunManager; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import com.dan.jasper.AircraftData; /** * 使用javabean作为数据源 * @author zdd * */ public class BeanCollectionDSReportServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 2174316885912562306L; private JRDataSource createReportDataSource(){ JRBeanCollectionDataSource dataSource; Collection resportRows = initializeMapArray(); dataSource = new JRBeanCollectionDataSource(resportRows); return dataSource; } private Collection initializeMapArray(){ ArrayList reportRows = new ArrayList(); reportRows.add(new AircraftData("n0", "zddn0", "female")); reportRows.add(new AircraftData("n1", "zddn1", "female")); reportRows.add(new AircraftData("n2", "zddn2", "female")); reportRows.add(new AircraftData("n3", "zddn3", "female")); return reportRows; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletOutputStream servletOutputStream = resp.getOutputStream(); InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/WEB-INF/classes/reports/AircraftReport.jasper"); try{ JRDataSource dataSource = createReportDataSource(); JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream,new HashMap(),dataSource ); resp.setContentType("application/pdf"); servletOutputStream.flush(); servletOutputStream.close(); }catch(Exception e){ StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); resp.setContentType("text/plain"); resp.getOutputStream().print(stringWriter.toString()); } } }
5.配置web.xml,就是配置servlet
6.运行项目
看一下我的运行结果:
1)JRBeanArrayDataSource:
2)JRBeanCollectionDataSource
小结:
其实这个实体对象作为数据源对于我们来说是很常用的,因为我们现在都是使用的hibernate,所以我们都会避免在其他地方直接与数据库进行交互,而这个对于实体对象数据源的支持满足了我们的要求。