如何在Spring MVC中用Jasper Report表格展示MySQL数据?报表无表格显示
嘿,我看了你代码里的问题,这几个地方是导致报表不显示表格的关键,咱们一步步来修复:
1. 修复DAO与Service的类型不匹配问题
你的DAO返回的是List<Employees>,但Service里却写成了List<NespakEmployees>,这会直接导致类型转换错误,数据根本传不到Controller里。另外,DAO用ALIAS_TO_ENTITY_MAP返回的是List<Map<String, Object>>,不是实体类列表,这也会影响后续的数据源处理。
修改EmployeeDaoImpl.java:
如果想返回实体类列表,不要用ALIAS_TO_ENTITY_MAP,而是直接把结果映射到Employees实体:
@SuppressWarnings("unchecked") public List<Employees> listAllNpEmp() { Session sessionHb = getSession(); // 用HQL查询实体,更符合ORM规范 String hql = "from Employees"; Query query = sessionHb.createQuery(hql); return query.list(); } // 要是坚持用原生SQL,就得添加实体映射: // String sql = "select * from employees"; // SQLQuery query = sessionHb.createSQLQuery(sql); // query.addEntity(Employees.class); // 关键:指定映射的实体类 // return query.list();
修改EmployeeeServiceImpl.java:
修正返回类型,和DAO保持一致:
@Service("EmployeesService") @Transactional public class EmployeeeServiceImpl implements EmployeesService{ @Autowired private EmployeeDao dao; public List<Employees> listNpEMp() { // 把NespakEmployees改成Employees return dao.listAllNpEmp(); } }
2. 优化Controller的报表处理逻辑
你之前在Controller里创建了JRBeanCollectionDataSource但没用到,反而JSP里重复处理数据,这容易出问题。建议直接在Controller里完成报表生成和输出,不用经过JSP,更符合Spring MVC的规范。
修改AppController.java:
@Controller @RequestMapping("/") @SessionAttributes("roles") public class AppController { @Autowired private SessionFactory sessionFactory; @Autowired EmployeesService npEmployeesService; @RequestMapping(value = {"nptestreport"}, method = RequestMethod.GET) public void empReportNp(HttpServletResponse response) throws Exception{ List<Employees> elist = npEmployeesService.listNpEMp(); // 用实体类列表创建Bean数据源 JRBeanCollectionDataSource dts = new JRBeanCollectionDataSource(elist, false); String jrxmlFile = sessionFactory.getCurrentSession().getServletContext().getRealPath("npReports/empReport.jrxml"); InputStream input = new FileInputStream(new File(jrxmlFile)); JasperReport jasperReport = JasperCompileManager.compileReport(input); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dts); // 设置响应头,让浏览器直接显示PDF response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline; filename=empReport.pdf"); JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream()); response.getOutputStream().flush(); response.getOutputStream().close(); } }
3. 修复JRXML报表文件(关键!)
你提供的jrxml代码不完整,Table_TD的样式没写完,而且要确保报表字段和实体类属性完全匹配(大小写敏感)。这里给你补全核心的表格配置:
<?xml version="1.0" encoding="UTF-8"?> <!-- 注意修改版本号匹配你的Jasper依赖,比如6.5.1 --> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="empReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBean Collection"/> <!-- 定义对应Employees实体的字段 --> <field name="npempid" class="java.lang.Integer"/> <field name="emp_code" class="java.lang.String"/> <field name="empname" class="java.lang.String"/> <field name="father_name" class="java.lang.String"/> <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF" lineSpacing="Single"> <box> <pen lineWidth="0.5" lineColor="#000000"/> <topPen lineWidth="0.5" lineColor="#000000"/> <leftPen lineWidth="0.5" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineColor="#000000"/> <rightPen lineWidth="0.5" lineColor="#000000"/> </box> </style> <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF" lineSpacing="Single"> <box> <pen lineWidth="0.5" lineColor="#000000"/> <topPen lineWidth="0.5" lineColor="#000000"/> <leftPen lineWidth="0.5" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineColor="#000000"/> <rightPen lineWidth="0.5" lineColor="#000000"/> </box> </style> <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF" lineSpacing="Single"> <box> <pen lineWidth="0.5" lineColor="#000000"/> <topPen lineWidth="0.5" lineColor="#000000"/> <leftPen lineWidth="0.5" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineColor="#000000"/> <rightPen lineWidth="0.5" lineColor="#000000"/> </box> </style> <!-- 表格组件 --> <componentElement> <reportElement x="0" y="30" width="555" height="20"/> <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd"> <datasetRun subDataset="Table Dataset"> <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression> </datasetRun> <!-- 员工ID列 --> <jr:column width="120"> <jr:columnHeader style="Table_CH" height="20"> <staticText> <reportElement x="0" y="0" width="120" height="20"/> <textElement textAlignment="Center"/> <text><![CDATA[员工ID]]></text> </staticText> </jr:columnHeader> <jr:detailCell style="Table_TD" height="20"> <textField> <reportElement x="0" y="0" width="120" height="20"/> <textFieldExpression><![CDATA[$F{npempid}]]></textFieldExpression> </textField> </jr:detailCell> </jr:column> <!-- 员工编码列 --> <jr:column width="120"> <jr:columnHeader style="Table_CH" height="20"> <staticText> <reportElement x="0" y="0" width="120" height="20"/> <textElement textAlignment="Center"/> <text><![CDATA[员工编码]]></text> </staticText> </jr:columnHeader> <jr:detailCell style="Table_TD" height="20"> <textField> <reportElement x="0" y="0" width="120" height="20"/> <textFieldExpression><![CDATA[$F{emp_code}]]></textFieldExpression> </textField> </jr:detailCell> </jr:column> <!-- 员工姓名列 --> <jr:column width="150"> <jr:columnHeader style="Table_CH" height="20"> <staticText> <reportElement x="0" y="0" width="150" height="20"/> <textElement textAlignment="Center"/> <text><![CDATA[员工姓名]]></text> </staticText> </jr:columnHeader> <jr:detailCell style="Table_TD" height="20"> <textField> <reportElement x="0" y="0" width="150" height="20"/> <textFieldExpression><![CDATA[$F{empname}]]></textFieldExpression> </textField> </jr:detailCell> </jr:column> <!-- 父亲姓名列 --> <jr:column width="165"> <jr:columnHeader style="Table_CH" height="20"> <staticText> <reportElement x="0" y="0" width="165" height="20"/> <textElement textAlignment="Center"/> <text><![CDATA[父亲姓名]]></text> </staticText> </jr:columnHeader> <jr:detailCell style="Table_TD" height="20"> <textField> <reportElement x="0" y="0" width="165" height="20"/> <textFieldExpression><![CDATA[$F{father_name}]]></textFieldExpression> </textField> </jr:detailCell> </jr:column> </jr:table> </componentElement> <!-- 表格数据集 --> <subDataset name="Table Dataset"> <field name="npempid" class="java.lang.Integer"/> <field name="emp_code" class="java.lang.String"/> <field name="empname" class="java.lang.String"/> <field name="father_name" class="java.lang.String"/> </subDataset> </jasperReport>
额外注意点
- 确保JasperReports的依赖版本和jrxml里的版本一致,避免兼容性问题;
- 检查数据库连接是否正常,DAO能正确查询到数据;
- 如果还是没显示,打开控制台看报错信息,通常是字段名不匹配或者数据源为空导致的。
内容的提问来源于stack exchange,提问作者shami
相关产品推荐
相关产品推荐

