Grails 数据绑定 - 带有列表的命令对象 [英] Grails data binding - command objects with Lists
问题描述
Grails 1.3.7
Grails 1.3.7
具有 List 内容的数据绑定 Command 对象有问题.示例命令:
Trouble with data binding Command objects that have List content. Example Command:
class Tracker {
String name
String description
List<Unit> units = new ArrayList()
}
class Unit {
String name
Long unitMax
Long unitMin
}
为跟踪器创建 GSP 具有单位字段.一个例子:
create GSP for Tracker has the Unit fields. One example:
<g:textField name="units[0].unitMax" value=""/>
TrackerController 保存方法:
TrackerController save method:
def save = { Tracker trackerInstance ->
trackerInstance = trackingService.saveOrUpdateTracker(trackerInstance)
}
但是,总是 java.lang.IndexOutOfBoundsException
But, always java.lang.IndexOutOfBoundsException
或者,如果我将控制器更新为:
Alternatively, if I update controller to:
def save = {
Tracker trackerInstance = new Tracker()
trackerInstance.properties = params
....
然后 groovy.lang.ReadOnlyPropertyException:无法设置只读属性:类的属性:com.redbrickhealth.dto.Tracker有任何想法吗?
Then groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: properties for class: com.redbrickhealth.dto.Tracker Any ideas?
GORM 和 Command 对象中的绑定之间似乎存在差异.
There seems to be a difference between binding in GORM vs Command objects.
也许我需要为 Unit 扩展和注册一个 PropertyEditorSupport?
Maybe I need to extend and register a PropertyEditorSupport for Unit?
-托德
推荐答案
Grails 需要一个具有现有列表的命令,该命令将填充来自请求的数据.
Grails requires an command with existing list, that will be filled with data from reques.
如果您知道确切的单位数,例如 3,您可以:
If you know exact number of units, say 3, you can:
class Tracker {
String name
String description
List<Unit> units = [new Unit(), new Unit(), new Unit()]
}
或使用 apache 公共集合中的 LazyList
or use LazyList from apache commons collections
import org.apache.commons.collections.ListUtils
import org.apache.commons.collections.Factory
class Tracker {
String name
String description
List<Unit> units = ListUtils.lazyList([], {new Unit()} as Factory)
}
这篇关于Grails 数据绑定 - 带有列表的命令对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!