我在使用 grails 3.1.4 使用 transient 属性绑定(bind)值时遇到问题。
以这个域为例:
class Domain {
Boolean b1
Boolean b2
Boolean b3
void setPropertyList(propertyList) {
if(propertyList.contains('someValue'))
this.b1 = true
}
static transients = ['propertyList']
static constraints = {
propertyList bindable: true
}
}
我想使用特定属性(此处:
propertyList
)进行数据绑定(bind)。此属性在数据绑定(bind)源中可用,但在我的域中不可用。所以我添加了一个 transient 和一个二传手。包括 transient propertyList
对于数据绑定(bind),我添加了 bindable
约束。二传手
setPropertyList
在数据绑定(bind)期间调用。结果域实例的属性按预期设置了所有属性。但是当我尝试保存结果实例时,我得到以下异常:
groovy.lang.GroovyRuntimeException: Cannot read write-only property: propertyList
at org.grails.validation.ConstrainedPropertyBuilder.doInvokeMethod(ConstrainedPropertyBuilder.java:74)
看起来 grails 在验证实例时遇到了一些麻烦。
任何想法如何解决这一问题?
最佳答案
经过一番调试,结果表明 grails 找不到 propertyList
的类型。因此跳过数据绑定(bind)。
添加 getter 有助于 grails 推断类型。
这避免了异常。
List<String> getPropertyList() {}
关于grails - 如何在 transient 上使用可绑定(bind)约束来避免 'Cannot read write-only property',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469320/