我正在努力用我的 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:
我不知道我做错了什么。有没有办法做到这一点?或者是否有另一种方法可以将我的报告连接到休眠数据源?