【问题标题】:Grails setting metaclass properties on object referenceGrails 在对象引用上设置元类属性
【发布时间】:2017-02-02 09:18:24
【问题描述】:

使用 Grails 域对象,我在尝试设置某些属性时偶然发现了一个问题。

var stepchild=parent.children.find{ it.id==xInt };
stepchild.metaClass.birthMom=biologicalMothersName;

parent.children.each{child->
    //when it gets to stepchild no metaclass property birthMom exists....
}

也没有在引用的对象上设置自定义错误

var stepchild=parent.children.find{ it.id==xInt };
stepchild.errors.rejectValue('parent',"Not biological parent");

parent.children.each{child->
    //when it gets to stepchild no errors....
}

我猜 find{} 不会返回对父子对象的真实引用?

【问题讨论】:

    标签: grails groovy metaclass


    【解决方案1】:

    对于您尝试在 stepChild 对象上设置属性/属性的方式,您应该使用 MetaClass.setAttribute() 方法:

    stepchild.metaClass.setAttribute(stepChild,'birthMom', biologicalMothersName)
    

    【讨论】:

    • 不完全确定,您没有提供太多关于域对象外观的代码。 parent 是 stepChild 域对象的实际属性吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多