如何将jasperreports子报表与grails jasper插件一起使用? [英] How to use jasperreports subreports with grails jasper plugin?

查看:134
本文介绍了如何将jasperreports子报表与grails jasper插件一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有grails jasper插件的子报表,我遵循这个网址的手册( http: //www.grails.org/plugin/jasper )。这是我的代码:

域名簿:

  class Book {

static belongsTo = Library

库库

字符串标题
字符串作者
字符串发布者
字符串类别

静态约束= {
title()
author()
publisher()
category()
}
}

域库:

 类库{

static hasMany = [books:Book]

字符串名称
String adresse
Date dateMaturity

static constraints = {
}

String toString()
{
return名称
}
}

在我的BookController中,我有:

  def createReport = {
def books = Book.list()
chain(控制器:碧玉',action:'index',model:[data:books],params:params)
}

在我的LibraryController中,我有:

  def createReport = {
def library = Library.list()
chain(controller:'jasper',action:'index',model:[data:library],params:params)
}

我的jasper部分是:

我有一个SubReport文件:books.jasper(获取书籍列表)。
也是一个MasterReport:library.jasper(获取图书馆列表)。



在我的MasterReport(图书馆)中,我添加了子报表,我希望为每个图书馆显示它包含的图书清单;这里是我的库代码:

 < parameter name =SUBREPORT_DIRclass =java.lang.StringisForPrompting =假> 
...
< field name =booksclass =java.util.Collection/>
...
< subreport isUsingCache =true>
< reportElement x =0y =25width =437height =100/>
< dataSourceExpression><![CDATA [new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ F {books})]]>< / dataSourceExpression>
< subreportExpression class =java.lang.String><![CDATA [$ P {SUBREPORT_DIR} +books.jasper]]>
< / subreportExpression>
< / subreport>

我有这个错误:

错误500:执行插件[jasper]中控制器[JasperController]的action [index]导致异常:net.sf.jasperreports.engine.fill.JRExpressionEvalException:评估表达式时出错:源文本:new net。 sf.jasperreports.engine.data.JRBeanCollectionDataSource($ F {books})

异常消息:未能延迟初始化角色集合:bookshelf.Library.books,没有会话或会话已关闭



感谢您的帮助。

报告只是期望对象列表。它不了解GORM查询。所以我们的方式是创建一个单独的对象列表,我们将其命名为'View Objects',然后将它们发送给jasper报告,而不是域类。

  class LibraryVO {
List books
String name
String adresse
Date dateMaturity
}

class bookVO {
String title
String author
String publisher
String category

b

您可以像

 列表数据= [] 

LibraryVo libVo = new LibraryVO(...)//在此处将其设为
libVo.books = [new BookVO(),new BookVO()]

data<< libVO

并将列表传递给jasper控制器

<
$ (chain(controller:'jasper',action:'index',model:[data:data],params:params)。


I would like to use subreports with grails jasper plugins, I followed the manual at this url (http://www.grails.org/plugin/jasper). Here is my code :

Domain Book :

class Book {

  static belongsTo = Library

    Library library

    String title
    String author
    String publisher
    String category

    static constraints={
        title()
        author()
        publisher()
        category()
    }
}

Domain Library :

class Library {

  static hasMany = [ books : Book ]

  String name
  String adresse
  Date dateMaturity

    static constraints = {
    }

        String toString()
    {
        return name
    }
}

In my BookController, I have :

def createReport = {
    def books = Book.list()
    chain(controller:'jasper',action:'index',model:[data:books],params:params)
}

In my LibraryController, I have :

def createReport = {
    def library = Library.list()
    chain(controller:'jasper',action:'index',model:[data:library],params:params)
}

My jasper part is :

I have a SubReport file : books.jasper (get a list of books).
Also a MasterReport : library.jasper (get a list of library).

In my MasterReport(library), I added subreport, I would like, for each library, show list of books it contains ; here is my library code :

<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
...
<field name="books" class="java.util.Collection"/>
...
<subreport isUsingCache="true">
<reportElement x="0" y="25" width="437" height="100"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{books})]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "books.jasper"]]>
</subreportExpression>
</subreport>

And I have this error :

Error 500: Executing action [index] of controller [JasperController] in plugin [jasper] caused exception: net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : Source text : new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{books})
Exception Message: failed to lazily initialize a collection of role: bookshelf.Library.books, no session or session was closed

Thank you for help.

解决方案

Jasper reports just expects the list of objects. It doesn't understand GORM queries. So the way we do is by creating a separate list of objects which we name as 'View Objects' and then send them to jasper reports instead of domain classes.

 class LibraryVO {
   List books
   String name
   String adresse
   Date dateMaturity
  }

 class bookVO {
  String title
    String author
    String publisher
    String category
  }

You can initializ the list like

 List data=[]

 LibraryVo libVo= new LibraryVO(...) // inalise it here
 libVo.books = [new BookVO(),new BookVO()]

 data << libVO

And pass the list to jasper controller

(chain(controller:'jasper',action:'index',model:[data:data],params:params).

这篇关于如何将jasperreports子报表与grails jasper插件一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆