0

我正在努力用我的 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:

我不知道我做错了什么。有没有办法做到这一点?或者是否有另一种方法可以将我的报告连接到休眠数据源?

4

1 回答 1

1

如果您使用 Spring MVC 在您的 JSP 中执行此操作不是正确的地方。@Transactional不错的选择是为您的服务方法使用合适的事务管理器。尝试sessionFactory.getCurrentSession()在您的 DAO 类中使用。它将返回与当前事务关联的会话。如果您想在您的 JSP 页面上延迟加载休眠实体,您可以在 web.xml 中配置 OpenSessionInView 过滤器。

参考见:http ://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/support/OpenSessionInViewFilter.html

于 2013-08-02T09:12:46.863 回答