Grails Jasper:从java.lang.Object中显示属性/ -ies

时间:2012-07-02 07:39:53

标签: grails jasper-reports ireport

关于我的关于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"/>

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

控制器链接的问题是域对象将不再附加到hibernate会话,因此是例外。

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

e.g。类似的东西:

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');
        }

    ...
}