grails命令对象出错

时间:2013-09-17 01:50:55

标签: grails

关于此错误的奇怪部分有时页面会生成错误,有时则不会。我无法弄清楚是什么导致它或原因:

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 Applicant”行。什么'java.util.LinkedHashMap不能转换为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

1 个答案:

答案 0 :(得分:2)

正如评论中指出的那样,您可能应该从occupation occupationDetailsinclude区块中移除LifestyleCommandApplicant({{1}}中没有该字段的余地1}})。