将对象列表绑定到Grails命令中?

时间:2015-05-25 19:17:46

标签: grails data-binding

我有2个命令对象,其中一个包含其他的

列表
@grails.validation.Validateable
class SongCommand {

    String title
    List<CoupletCommand> coupletCommandList = [].withLazyDefault {new CoupletCommand()}
    Boolean isChorusRepeat

    static constraints = {
        title blank: false, size: 1..129
        coupletCommandList validator: { couplets, obj ->
            def isValid = true
            couplets.each {
                isValid = isValid && it.validate()
            }
            return isValid
        }
    }
}
class CoupletCommand {

    Integer coupletPosition
    String coupletText
    Boolean isChorus

    static constraints = {
        coupletText blank: false, size:20..700
        isChorus nullable: true
    }
}

从前端到控制器我传递了类似的东西

[coupletText:2342342352342, coupletPosition:1, isChorus:false]
[coupletText:frfsdfsdf, coupletPosition:2, isChorus:true]
......
[coupletText:sd9f9s9df9, coupletPosition:n, isChorus:false]

尝试在控制器上保存对联列表时

def saveCouplets() {
        def songCommand = new SongCommand()
        // bad, but not necessary now
        def count = session['songId'] as Integer
        def bindingMap
        for (int i = 1; i <= count; i++) {
            bindingMap = [coupletText: params['coupletText-' + i],
                          coupletPosition: params['coupletPosition-' + i],
                          isChorus: (params['isChorus-' + i]) ? params['isChorus-' + i] : false]
            songCommand.coupletCommandList.add(bindingMap)
        }

        //Validate it
        //if valid
        if (songCommand.validate()) {
            coupletService.addCouplets(bindingMap, params)
        } else {
            render model: [command: songCommand]
        }
        //else render view show model [command: command]
    }

我收到错误消息

  

groovy.lang.MissingMethodException   信息   没有方法签名:java.util.LinkedHashMap.validate()适用于参数类型:()values:[]可能的解决方案:wait(),values(),values()

1 个答案:

答案 0 :(得分:0)

CoupletCommand需要实现Validateable或者也需要注释。其他一切似乎都很好