【问题标题】:Error in grails command objectgrails 命令对象中的错误
【发布时间】:2013-09-17 01:50:54
【问题描述】:

这个错误的奇怪之处在于页面有时会生成错误,有时不会。我不知道是什么原因造成的或为什么:

errors.GrailsExceptionResolver ClassCastException occurred when processing request: [GET] /birthFamily/aboutYouLifestyle
java.util.LinkedHashMap cannot be cast to groovy.lang.Closure. Stacktrace follows:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to groovy.lang.Closure
    at com.nrfa.LifestyleCommand$__clinit__closure1.doCall(Wizard.groovy:302)
    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:724)

有问题的命令对象:

@Validateable
public  class LifestyleCommand {
    Integer applicantNumber;
    Smoke smoke
    Drink drink
    Occupation occupation
    Set <String> hobby
    String occupationDetails

    static constraints = {
        importFrom Applicant, include:["smoke", "drink", "occupation", "occupationDetails", "applicantNumber"]
    }
}

堆栈跟踪中的第 302 行是命令对象约束中的“importFrom申请人”行。 'java.util.LinkedHashMap cannot be cast to groovy.lang.Closure'是什么意思?

当我将约束替换为在申请人域中找到的约束的副本时,它也可以正常工作:

static constraints = {

    applicantNumber (blank:false, nullable:false, range:1..2)
    smoke (blank:true, nullable:true)
    drink (blank:true, nullable:true)
    occupation (blank:true, nullable:true)
    occupationDetails (blank:true, nullable:true, maxSize:150)
}

编辑:

这里是申请人的相关部分,它是一个域对象:

class Applicant {

    static hasMany = [hobby:Hobby, ethnicity:Ethnicity]

    Integer applicantNumber
    String gender
    Integer yearBorn
    EyeColor eyeColor
    HairColor hairColor
    Integer heightFeet
    Integer heightInches

    static constraints = {
        applicantNumber (blank:false, nullable:false, range:1..2)
        gender (blank:true, nullable:true, validator: { value ->
            if (value != null && value != '' && (value != "M" && value != "F")) {
                return 'applicant.invalid.gender'
            }
        })
        yearBorn (blank:true, nullable:true, validator: { value ->
            if (value != null && (value < 1920 || value > 2014)) {
                return 'applicant.invalid.yearBorn'
            }
        })
        eyeColor (blank:true, nullable:true)
        hairColor (blank:true, nullable:true)
        smoke (blank:true, nullable:true)
        drink (blank:true, nullable:true)
        heightFeet (blank:true, nullable:true, validator: { value ->
            if (value != null && (value < 3 || value > 7)) {
                return 'applicant.invalid.heightFeet'
            }
        })
        heightInches (blank:true, nullable:true, validator: { value ->
            if (value != null && (value < 1 || value > 12)) {
                return 'applicant.invalid.heightInches'
            }
        })
    }
}

向导是我的控制器。有一个操作来处​​理表单请求,该请求提交要填充到 LifestyleCommand 对象中的数据。我只是在没有填写任何字段的情况下提交页面,所以一切都是 null/empty 这是有效的。

我正在运行 grails 2.2.4

【问题讨论】:

  • 你也可以在问题中按原样发布Applicant吗?
  • 什么是Wizard.groovy
  • 看起来它正在尝试将 importFrom 的“include”部分转换为“last-argument”闭包。但是,这没有任何意义,因为 importFrom 采用了包含列表参数,因为它被添加到 2.0.2(根据文档)grails.org/doc/2.0.2/guide/validation.html#constraints 我想看看这个绑定存在哪些参数,以及什么版本的 Grails 正在使用中。
  • 我已经添加了申请者域并提供了向导代码的解释。
  • 您在Applicant 中没有occupationoccupationDetails 及其约束吗?

标签: grails


【解决方案1】:

正如 cmets 中所指出的,您可能应该从 include 块的 LifestyleCommand 约束中删除 occupationoccupationDetailsApplicant 中没有该字段的约束)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2010-12-24
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    相关资源
    最近更新 更多