好吧我之前问过一个问题,但对此不太确定。所以我继续等到现在再问一次。添加实例的Grails控制器
主要问题
如何通过控制器加入域的一个新的实例?我创建了一个名为gather的函数来读取包含数据的文件,然后使用特定信息创建一个新的Book,但是它根本不会将其添加到数据库中。
我目前有一个控制器(bookController)和它的域。
我的域名很简单:
class Book {
static belongsTo = Author
String toString() { bookNumber }
Author bookAuthor
String title
static constraints = {
bookAuthor()
title()
}
}
我只是“产生”我的看法,所以我有基本的创建,编辑,列表和显示。我继续并在控制器中添加了我自己的集合。对于gsp,我只是复制了'list.gsp',因为我只想让用户在收集函数完成后查看书籍列表。
这里是我的控制器看起来像(仅仅是基本的产生一加集):
package bookdemo
import bookClient
class BookController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def index = {
redirect(action: "list", params: params)
}
def gather = {
def w = new bookClient() //bookClient will gather books from txt files
def hosts = ["localhost"] //host to connect to
w.queryData(hosts) //grab information and parse
def abc = w.bookList //list of books
w.printData(abc) //print out list of books to make sure its not null
int numberOfBooks = abc.size() //list size
//create book list and return it
numberOfBooks.times {
def bookInstance = new Book(Author:"$abc.author", Title:"$abc.title")
return [bookInstance: bookInstance]
}
//params to show once adding books
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[bookInstanceList: book.list(params), bookInstanceTotal: book.count()]
}
def list = {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[bookInstanceList: book.list(params), bookInstanceTotal: book.count()]
}
def create = {
def bookInstance = new Book()
bookInstance.properties = params
return [bookInstance: bookInstance]
}
def save = {
def bookInstance = new Book(params)
if (bookInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'book.label', default: 'Book'), bookInstance.id])}"
redirect(action: "show", id: bookInstance.id)
}
else {
render(view: "create", model: [bookInstance: bookInstance])
}
}
def show = {
def bookInstance = book.get(params.id)
if (!bookInstance) {
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
redirect(action: "list")
}
else {
[bookInstance: bookInstance]
}
}
def edit = {
def bookInstance = book.get(params.id)
if (!bookInstance) {
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
redirect(action: "list")
}
else {
return [bookInstance: bookInstance]
}
}
def update = {
def bookInstance = book.get(params.id)
if (bookInstance) {
if (params.version) {
def version = params.version.toLong()
if (bookInstance.version > version) {
bookInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'book.label', default: 'Book')] as Object[], "Another user has updated this Book while you were editing")
render(view: "edit", model: [bookInstance: bookInstance])
return
}
}
bookInstance.properties = params
if (!bookInstance.hasErrors() && bookInstance.save(flush: true)) {
flash.message = "${message(code: 'default.updated.message', args: [message(code: 'book.label', default: 'Book'), bookInstance.id])}"
redirect(action: "show", id: bookInstance.id)
}
else {
render(view: "edit", model: [bookInstance: bookInstance])
}
}
else {
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
redirect(action: "list")
}
}
def delete = {
def bookInstance = book.get(params.id)
if (bookInstance) {
try {
bookInstance.delete(flush: true)
flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
redirect(action: "list")
}
catch (org.springframework.dao.DataIntegrityViolationException e) {
flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
redirect(action: "show", id: params.id)
}
}
else {
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])}"
redirect(action: "list")
}
}
}
普惠制显示出来,但由于某种原因没有加入我的新书。当我添加一个println来测试信息是否在列表中时,它会显示具有正确信息的打印。所以我很困惑,为什么它不是“创建”新书实例并将其添加到数据库中。
有什么建议吗?
作者编辑
域类:
class Author {
static hasMany = [books:Book]
String authorName
String notes
String toString() { authorName }
static constraints = {
machineName()
notes(maxSize:500)
}
}
谢谢你指出,但这是我第一次尝试,并没有改变结果。 – StartingGroovy 2010-09-23 20:12:17
那么除非你使用这种方法,否则他们绝对不会保存。你一定还有其他的错误。检查save()的返回值是否为空。如果是这样,有一个验证错误阻止他们坚持 – leebutts 2010-09-24 06:20:44
谢谢,我会检查并回发。 – StartingGroovy 2010-09-24 18:49:31