当关系被定义为列表时,无法在Grails json视图中迭代一对多关系



我对Grails的新json视图和一对多关联有问题。以下代码正在工作:

Foo:

class Foo {
    String name 
    static hasMany = [bars:Bar]
    static constraints = {
    }
}

等级栏:

class Bar {
    String name
    Foo foo
    static constraints = {
    }
}

foo的Json视图(show.gson):

model {
    Foo foo
}
json {
    foo g.render(template:"foo", model:[foo:foo])
}

_foo.gson模板:

model {
    Foo foo
}
json {
    name foo.name
    bars foo.bars.collect { it.id }
}

但是,如果我将foo类更改为使用List,则如下所示:

class Foo {
    String name
    List bars
    static hasMany = [bars:Bar]
    static constraints = {
    }
}

我得到以下错误:

[静态类型检查]-没有这样的属性:类的id:java.lang.Object@第13行第29列。bars foo.bars.collect{it.id}

我相信有办法解决这个问题,但我想知道是否有人经历过这种情况,知道为什么使用List时它不起作用。

非常感谢你的帮助。

如在https://github.com/grails/grails-views/issues/38,集合需要声明为List<Bar> bars,而不是List bars

最新更新