2012-07-02 48 views
0

顯示屬性/ -ies我有一個關於對我的Grails曾鈺成報告,其中我應該顯示具有這種性質Grails的碧玉:從java.lang.Object繼承

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 

這裏是位於碧玉報告[的.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"/> 

我該如何解決這個問題的字段名稱?

+0

請問您可以發佈相關的控制器代碼嗎? –

+0

@JonoB我按照您的要求編輯它。 –

回答

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

    ... 
} 
+0

我會把它放在控制器,視圖,服務或模型本身嗎? –

+0

查看更新的答案。 –

+0

我試過了你的答案,但是它在我的瀏覽器'文件未找到'時返回一個錯誤消息'Firefox在localhost找不到文件:9090/Portal/schedule/...;當我想以HTML格式查看報告時。什麼可能是錯誤?謝謝。 –