grails - 将对象列表绑定(bind)到 Grails 命令中?

标签 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
    }
}

从前端到 Controller 我通过了这样的东西
[coupletText:2342342352342, coupletPosition:1, isChorus:false]
[coupletText:frfsdfsdf, coupletPosition:2, isChorus:true]
......
[coupletText:sd9f9s9df9, coupletPosition:n, isChorus:false]

当尝试在 Controller 上保存对联列表时
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 Message No signature of method: java.util.LinkedHashMap.validate() is applicable for argument types: () values: [] Possible solutions: wait(), values(), values()

最佳答案

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

关于grails - 将对象列表绑定(bind)到 Grails 命令中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30444714/

相关文章:

grails - 使用Groovy从OpenLDAP服务器读取和保存二进制图像

Grails/GORM : difference between lazy: false & fetchMode eager

grails - Grails “render as XML”抛出java.lang.NullPointerException-我做错了什么?

c# - 为 RadioButtonList 项目添加工具提示

xml - Delphi XML 绑定(bind)向导和可选元素

c# - 使用 DataBinding 的 ListBox 填充速度极慢

Grails 如何解析文本文件?

grails - Grails参数传递

c++ - 使用 C++ 对象 Q_PROPERTY 绑定(bind)复选框 'checked' 属性

.net - 从 DataTemplate 访问父 DataContext