我试图通过使用数据绑定的表单更新hasMany
关系,但我在params
中看到的数据看起来不正确。Grails选择标记结果
域类:
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)
}
}
这给了我一个错误:
Error 200: org.springframework.beans.NotReadablePropertyException: Invalid property 'currentDeviceData' of bean class [java.lang.String]: Bean property 'currentDeviceData' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the 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}"/>
但是,这给了我丢失的方法错误,虽然它没有解决的问题具有索引而不是返回实际值。
任何有关这里发生了什么的想法以及我如何解决它?
顺便说一句,我正在运行Grails的1.0.4版本。是的,我很想升级它,但我不能。
谢谢!
当我看到您的帖子时,我已经走到了我发布的解决方案的路径。就我而言,如果我使用'?'或'*',似乎没有什么区别,尽管我看到他们有不同的目的。所以,任何一个工作。谢谢! – 2012-07-18 11:50:24
您的最终解决方案看起来太复杂了,应该不是这样,我相信问题隐藏在其他地方。 – 2012-07-18 13:13:00