Grails数据绑定 - 带有列表的命令对象 [英] Grails data binding - command objects with Lists

查看:123
本文介绍了Grails数据绑定 - 带有列表的命令对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails 1.3.7

数据绑定遇到麻烦带有列表内容的Command对象。示例命令:

  class Tracker {
字符串名称
字符串描述
List< Unit> units = new ArrayList()
}

class Unit {
String name
Long unitMax
Long unitMin
}

为追踪器创建GSP具有单位字段。例如:

 < g:textField name =units [0] .unitMaxvalue =/> 

TrackerController保存方法:

  def save = {Tracker trackerInstance  - > 
trackerInstance = trackingService.saveOrUpdateTracker(trackerInstance)
}

但是,总是java .lang.IndexOutOfBoundsException



或者,如果我将控制器更新为:

  def save = {
Tracker trackerInstance = new Tracker()
trackerInstance.properties = params
....

然后groovy.lang.ReadOnlyPropertyException:无法设置只读属性:类的属性:com.redbrickhealth.dto.Tracker
任何想法?


GORM与Command对象的绑定似乎有所不同。



也许我需要扩展并注册PropertyEditorSupport for Unit?

-Todd

解决方案

Grails需要一个带有现有列表的命令,它将充满来自请求的数据。



如果你知道确切的单位数,比如3,你可以:

  class Tracker {
字符串名称
字符串描述
List< Unit>单位= [新单位(),新单位(),新单位()]
}

或从apache commons集合中使用LazyList

  import org.apache.commons.collections.ListUtils 
import org。 apache.commons.collections.Factory $ b $ class Tracker {
字符串名称
字符串描述
List< Unit> units = ListUtils.lazyList([],{new Unit()} as Factory)
}


Grails 1.3.7

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
}

create GSP for Tracker has the Unit fields. One example:

<g:textField name="units[0].unitMax" value=""/>

TrackerController save method:

 def save = { Tracker trackerInstance ->
   trackerInstance = trackingService.saveOrUpdateTracker(trackerInstance)
 }

But, always java.lang.IndexOutOfBoundsException

Alternatively, if I update controller to:

def save = {
   Tracker trackerInstance = new Tracker()
   trackerInstance.properties = params
   ....

Then groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: properties for class: com.redbrickhealth.dto.Tracker Any ideas?

There seems to be a difference between binding in GORM vs Command objects.

Maybe I need to extend and register a PropertyEditorSupport for Unit?

-Todd

解决方案

Grails requires an command with existing list, that will be filled with data from reques.

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()]
}

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆