grails数据绑定集合

时间:2014-07-12 19:48:20

标签: grails grails-2.0

任何想法为什么在Grails框架 - 参考文档中关于绑定到集合和映射的下一个示例失败,并显示以下异常???

class Band {
    String name
    static hasMany = [albums: Album]
    List albums
}
class Album {
    String title
    Integer numberOfTracks
}

def bindingMap = [name: 'Genesis', 
                  'albums[0]': [title: 'Foxtrot', numberOfTracks: 6], 
                  'albums[1]': [title: 'Nursery Cryme', numberOfTracks: 7]]
def band = new Band(bindingMap)

assert band.name == 'Genesis'
assert band.albums.size() == 2
assert band.albums[0].title == 'Foxtrot'
assert band.albums[0].numberOfTracks == 6
assert band.albums[1].title == 'Nursery Cryme'
assert band.albums[1].numberOfTracks == 7


≫ groovy.lang.MissingPropertyException: No such property: albums[0] for class: Band Possible solutions: albums at Script1.run(Script1.groovy:15) at org.grails.plugins.console.ConsoleService.eval(ConsoleService.groovy:57) at org.grails.plugins.console.ConsoleService.eval(ConsoleService.groovy:37) at org.grails.plugins.console.ConsoleController.execute(ConsoleController.groovy:32) at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198) at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63) at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53) at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49) at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:722) 

2 个答案:

答案 0 :(得分:1)

如果你有这样的事情:

专辑:

// grails-app/domain/demo/Album.groovy
package demo

class Album {
    String title
    Integer numberOfTracks
}

频段:

// grails-app/domain/demo/Band.groovy
package demo

class Band {
    String name
    static hasMany = [albums: Album]
    List albums
}

本单元测试将通过:

// test/unit/demo/BandSpec.groovy
package demo

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(Band)
class BandSpec extends Specification {

    void "test constructor binding"() {
        given:
        def bindingMap = [name: 'Genesis',
                          'albums[0]': [title: 'Foxtrot', numberOfTracks: 6],
                          'albums[1]': [title: 'Nursery Cryme', numberOfTracks: 7]]
        when:
        def band = new Band(bindingMap)

        then:
        band.name == 'Genesis'
        band.albums.size() == 2
        band.albums[0].title == 'Foxtrot'
        band.albums[0].numberOfTracks == 6
        band.albums[1].title == 'Nursery Cryme'
        band.albums[1].numberOfTracks == 7
    }
}

该代码位于https://github.com/jeffbrown/bindingissue的项目中。

可能导致您遇到问题的一件事是,Band是否未被识别为域类。确保在grails-app / domain / somewhere下定义了类。 Grails提供的特殊数据绑定通常在构造函数中不可用。域类是一个例外。

我希望有所帮助。

答案 1 :(得分:0)

我想问题是 - 在你的绑定地图中 - 你选择了一个属性“albums [0]”,而不是使用一个集合“albums”并给它两个条目。 Groovy正在寻找一个列表,但你已经将这个表达式作为一个属性。首先创建相册,然后将它们添加到相册列表。

像这样:

def bindingMap = [
  name: 'Genesis', 
  'albums': [
    [title: 'Foxtrot', numberOfTracks: 6], 
    [title: 'Nursery Cryme', numberOfTracks: 7]
  ]
]