0

我在 Grails 上的 Jasper 报告中有这个问题,我应该显示具有此属性的特定模型的内容

class Schedule {
  Subject subject
  Room room // another class having only a single property [room:String]
  DayOfWeek day //days of the week having only a single property [day:String]
  String timeStart
  String timeEnd
  //constraints
}
class Subject {
   Course course
   String section
   static hasMany = [schedule: Schedule]
   // constraints
}

问题是,当我尝试从jasperReport将要获取的控制器中调用操作时Schedule.list(),我得到了这个错误作为回报

URI     /Portal/jasper/index
Class   org.hibernate.LazyInitializationException
Message could not initialize proxy - no Session

这是控制器和视图代码。

// ScheduleController
def report() {
   List scheduleList = Schedule.list()
   chain(controller:'jasper', action:'index', params:params,
         model:[data:scheduleList])
}

//view
<g:jasperReport jasper="schedule_list"
                controller="schedule"
                action="report"
                format="pdf, html"
                name="Schedule List"
                description=" " />

为了解决这个问题,我尝试使用下面的这个理论来解析域中的属性。但作为回报,报告会返回报告null中具有正确记录数的所有字段。

List scheduleList = Schedule.list().collect {
   [cell:
      [it.subject.toString(),
       it.room.toString(),
       it.day.toString(),
       it.timeStart,
       it.timeEnd
      ],id: it.id
   ]    
} as List

这是位于 jasper 报告 [.jrxml] 中的字段名称

<field name="subject" class="java.lang.String"/>
<field name="room" class="java.lang.String"/>
<field name="day" class="java.lang.String"/>
<field name="timeStart" class="java.lang.String"/>
<field name="timeEnd" class="java.lang.String"/>

我该如何解决这个问题?

4

1 回答 1

0

控制器链接的问题是域对象将不再附加到休眠会话,因此出现异常。

使用 JasperService(来自您的控制器)生成报告,而不是链接,如下所示:http: //www.grails.org/plugin/jasper

例如:

class ScheduleController {

    def jasperService

    ...

    def report() {

        // Get the report data and build the report.
        List scheduleList = Schedule.list()
        JasperReportDef reportDef = jasperService.buildReportDefinition(params, request.getLocale(), [data:sheduleList])

        // Non-inline reports (e.g. PDF)
        if (!reportDef.fileFormat.inline && !reportDef.parameters._inline)
        {
            response.setHeader("Content-disposition", "attachment; filename="+(reportDef.parameters._name ?: reportDef.name) + "." + reportDef.fileFormat.extension);
            response.contentType = reportDef.fileFormat.mimeTyp
            response.characterEncoding = "UTF-8"
            response.outputStream << reportDef.contentStream.toByteArray()
        }
        else
        {
            // Inline report (e.g. HTML)
            render(text: reportDef.contentStream, contentType: reportDef.fileFormat.mimeTyp, encoding: reportDef.parameters.encoding ? reportDef.parameters.encoding : 'UTF-8');
        }

    ...
}
于 2012-07-02T21:31:47.223 回答