0

I have this problem regarding my Jasper Report on Grails wherein I should display the contents of a particular model having this properties

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
}

The problem is that when I try to call the action from the controller where the jasperReport will be getting the Schedule.list(), I get this error in return

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

Here's the controller and view code.

// 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=" " />

In attempt to resolve this problem, I tried to parse the properties in the domain using this theory below. But in return the report returns all fields on the report null having the correct number of records.

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

Here's the fields name located in the jasper report [.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"/>

How can I resolve this problem?

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 に答える