也許這是持久性和域對象的問題。所以我有一個可以添加到一個人的手動註釋列表。我個人類類似於這樣(我裹在瞬態對象忽略持久性):域對象字符串列表 - 實時添加
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域對象和集合是否存在持久性問題?這可能是我的一個簡單問題!
感謝您的幫助。
本示例使用作者和圖書之間的一對多關係:http://www.2paths.com/2009/10/01/one-to-many-relationships-in-grails-forms/我想要做類似的事情,但很明顯不會與書籍對象。相反,我想使用字符串鍵入的筆記。 – user82302124 2012-02-24 16:30:42