这可能是持久性和域对象的问题。所以我有一个可以添加到一个人的手册备注列表。我的person类与此类似(我将对象包装在瞬态中以忽略持久性):
class Person {
...
List<String> notes = new ArrayList<String>()
...
}
当我用笔记更新一个人时(视图上的文本字段将允许添加注释),我想做一些简单的事情,比如将新笔记添加到与该人绑定的数组列表中:
class PersonController {
...
def update() {
def contactInstance = Contact.get(params.id)
if (!contactInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'contact.label', default: 'Contact'), params.id])
redirect(action: "list")
return
}
if (params.version) {
def version = params.version.toLong()
if (contactInstance.version > version) {
contactInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'contact.label', default: 'Contact')] as Object[],
"Another user has updated this Contact while you were editing")
render(view: "edit", model: [contactInstance: contactInstance])
return
}
}
contactInstance.properties = params
/**
* Check for inactive - Then flag with user and date tag
*/
if(params.isActive == null) {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Date date = new Date();
contactInstance.properties.isActiveNote = "Made inactive by " + session.user + " on " + dateFormat.format(date) + "."
}
/**
* Date stamp of the note itself
*/
if(params.notes.equals("")) {}
else {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Date date = new Date();
//contactInstance.properties.notes = contactInstance.properties.notes + " " + params.notes + " - (" + dateFormat.format(date) + " " + session.user + ");"
contactInstance.allThese.add(contactInstance.properties.notes)
println(contactInstance.allThese)
}
if (!contactInstance.save(flush: true)) {
render(view: "edit", model: [contactInstance: contactInstance])
return
}
flash.message = message(code: 'default.updated.message', args: [message(code: 'contact.label', default: 'Contact'), contactInstance.id])
redirect(action: "show", id: contactInstance.id)
}
...
}
从我的gsp调用更新:
<g:actionSubmit class="save" action="update" value="${message(code:'default.button.update.label', default: 'Update')}" />
但它似乎只是用一个音符存储数组。 Grails域对象和集合是否存在持久性问题?这可能是我个人的一个简单问题!
感谢您的帮助。
答案 0 :(得分:0)
如果您的视图包含多个<input>
字段或<textarea>
具有相同名称(“注释”,以匹配您的Domain类中的字段名称),Grails会自动将值绑定到您的{ {1}}数组。
您还可以查看新的notes
方法,该方法允许您遍历请求参数,然后将它们单独添加到您的数组中。