【发布时间】:2013-08-01 16:49:41
【问题描述】:
我正在努力用我的 jsp 文件检索我的休眠会话。该会话将用作我的报告的数据源。
这是我的 JSP 文件:
<%@ page language="java" import="net.sf.jasperreports.engine.*,net.sf.jasperreports.engine.export.*,net.sf.jasperreports.engine.query.JRHibernateQueryExecuterFactory,org.hibernate.SessionFactory" %>
<%@ page import="java.sql.*,java.io.*,java.util.HashMap" %>
<%
String filename = "/WEB-INF/pages/tddd27.jrxml";
String reporttype = "html";
SessionFactory sessionFactory;
String path = application.getRealPath("/");
HashMap parameters = new HashMap();
parameters.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, sessionFactory.getCurrentSession());
JasperReport jasperReport =JasperCompileManager.compileReport(path + "/" + filename);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
System.out.println("Report Created...");
OutputStream ouputStream = response.getOutputStream();
JRExporter exporter = null;
if( "pdf".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/pdf");
exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
else if( "rtf".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/rtf");
response.setHeader("Content-Disposition", "inline; filename=\"file.rtf\"");
exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
else if( "html".equalsIgnoreCase(reporttype) )
{
exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
else if( "xls".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/xls");
response.setHeader("Content-Disposition", "inline; filename=\"file.xls\"");
exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
else if( "csv".equalsIgnoreCase(reporttype) )
{
response.setContentType("application/csv");
response.setHeader("Content-Disposition", "inline; filename=\"file.csv\"");
exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
}
try
{
exporter.exportReport();
}
catch (JRException e)
{
throw new ServletException(e);
}
finally
{
if (ouputStream != null)
{
try
{
ouputStream.close();
}
catch (IOException ex)
{
}
}
}
%>
这是我得到的错误:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 14 in the jsp file: /WEB-INF/pages/reportpage.jsp
The local variable sessionFactory may not have been initialized
11:
12: String path = application.getRealPath("/");
13: HashMap parameters = new HashMap();
14: parameters.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, sessionFactory.getCurrentSession());
15:
16: JasperReport jasperReport =JasperCompileManager.compileReport(path + "/" + filename);
17:
我不知道我做错了什么。有没有办法做到这一点?或者是否有其他方法可以将我的报表连接到休眠数据源?
【问题讨论】:
-
您正在使用 Spring MVC。为什么要使用 JSP 来实现控制器?使用弹簧控制器。 JSP 不应包含 Java 代码。也就是说,信息非常明确。你还没有初始化变量
sessionFactory。 -
好的,谢谢您的回答。我将尝试将其实现为弹簧控制器。
标签: hibernate jsp spring-mvc jasper-reports