Tod*_*d M 2 grails grails-controller
Grails 1.3.7
数据绑定问题具有List内容的Command对象.示例命令:
class Tracker {
String name
String description
List<Unit> units = new ArrayList()
}
class Unit {
String name
Long unitMax
Long unitMin
}
Run Code Online (Sandbox Code Playgroud)
为Tracker创建GSP具有单位字段.一个例子:
<g:textField name="units[0].unitMax" value=""/>
Run Code Online (Sandbox Code Playgroud)
TrackerController保存方法:
def save = { Tracker trackerInstance ->
trackerInstance = trackingService.saveOrUpdateTracker(trackerInstance)
}
Run Code Online (Sandbox Code Playgroud)
但是,始终是java.lang.IndexOutOfBoundsException
或者,如果我将控制器更新为:
def save = {
Tracker trackerInstance = new Tracker()
trackerInstance.properties = params
....
Run Code Online (Sandbox Code Playgroud)
然后groovy.lang.ReadOnlyPropertyException:无法设置readonly属性:class的属性:com.redbrickhealth.dto.Tracker任何想法?
GORM与Command对象之间的绑定似乎有区别.
也许我需要为Unit扩展并注册PropertyEditorSupport?
-Todd
从Groovy 1.8.7开始,List接口有一个名为的方法withLazyDefault,可以用来代替apache commons ListUtils:
List<Unit> units = [].withLazyDefault { new Unit() }
Run Code Online (Sandbox Code Playgroud)
Unit每次units使用不存在的索引访问时,这将创建一个新实例.
有关更多详细信息,请参阅withLazyDefault的文档.几天前我还写了一篇关于此事的小博文.
| 归档时间: |
|
| 查看次数: |
6535 次 |
| 最近记录: |