0

我在使用 grails 3.1.4 使用瞬态属性绑定值时遇到问题。

以这个域为例:

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进行数据绑定。此属性在数据绑定源中可用,但在我的域中不可用。所以我添加了一个瞬态和一个二传手。propertyList为了包含数据绑定的瞬态,我添加了bindable约束。

setPropertyList在数据绑定期间调用setter 。结果域实例的属性按预期设置了所有属性。但是当我尝试保存结果实例时,我得到以下异常:

groovy.lang.GroovyRuntimeException: Cannot read write-only property: propertyList
    at org.grails.validation.ConstrainedPropertyBuilder.doInvokeMethod(ConstrainedPropertyBuilder.java:74)

看起来 grails 在验证实例时遇到了一些麻烦。

任何想法如何解决这一问题?

4

1 回答 1

0

经过一些调试,结果发现,grails 找不到类型propertyList,因此跳过了数据绑定。

添加 getter 有助于 grails 推断类型。这避免了异常。

List<String> getPropertyList() {}
于 2016-04-07T11:30:59.430 回答