【发布时间】:2012-07-17 13:48:01
【问题描述】:
我正在尝试使用数据绑定通过表单更新hasMany 关系,但我在params 中获得的数据似乎不正确。
领域类:
class CustomerSite {
static hasMany = [dhrs:DeviceHistoryRecord];
static mapping = {
id generator:'sequence', params:[sequence:'cs_seq']
}
...
}
编辑视图:
...
<g:select name="dhrs" id="dhrs"
from="${DeviceHistoryRecord.list()}"
multiple="multiple"
optionKey="id"
value="${customerSiteInstance?.dhrs}"/>
控制器:
def update = {
def customerSiteInstance = CustomerSite.get( params.id )
if(customerSiteInstance) {
customerSiteInstance.properties = params
String flashMsg = new String();
flash.message = "";
if(!customerSiteInstance.hasErrors() && customerSiteInstance.save()) {
flash.message += "Customer Site ${customerSiteInstance.toString()} updated"
redirect(action:show,id:customerSiteInstance.id)
}
else {
flash.message = flashMsg
render(view:'edit',model:[customerSiteInstance:customerSiteInstance])
}
}
else {
flash.message = "Customer Site not found with id ${params.id}"
redirect(action:edit,id:params.id)
}
}
这给了我一个错误:
错误 200:org.springframework.beans.NotReadablePropertyException:bean 类 [java.lang.String] 的无效属性“currentDeviceData”:Bean 属性“currentDeviceData”不可读或具有无效的 getter 方法:返回类型是否为getter 是否匹配 setter 的参数类型?
在控制器代码的这一行:
if(!customerSiteInstance.hasErrors() && customerSiteInstance.save()) {
这对我来说没有任何意义,但我做了一些愚弄(实际上很多),最终发现g:select 正在将一组索引传递给参数。
视图输出我认为正确的代码:
<select name="dhrs" id="dhrs" multiple="multiple" >
<option value="2421" >801122</option>
<option value="2422" >801123</option>
...
如果我要在列表的索引 0 和索引 1 处选择项目,它不会像我预期的那样传入一组“2421”和“2422”。它传入“0”和“1”。更糟糕的是,在我运行它之后,当我返回编辑页面并再次运行它时,这次选择索引 8 处的内容,它将有“8”......还有“0”和“1” " 上次的。
在这里看了一圈,发现Selecting multiple values from select tag - Grails,还有一些其他的想法,包括做出这样的改变:
<g:select name="dhrs.id"
from="${DeviceHistoryRecord.list()}"
multiple="multiple"
optionKey="id"
value="${customerSiteInstance?.dhrs*.id}"/>
但这给了我一个缺少方法的错误,尽管它确实解决了返回索引而不是实际值的问题。
关于这里发生了什么以及如何解决它的任何想法?
顺便说一句,我正在运行 1.0.4 版的 Grails。是的,我很想升级它,但我不能。
谢谢!
【问题讨论】:
标签: grails data-binding