Grails控制器添加实例

时间:2010-09-21 21:50:52

标签: grails controller dns show add

好吧,我之前问了一个问题,但对此并不十分肯定。所以我继续等到现在再问一次。

主要问题

如何通过控制器添加域的新实例?我创建了一个名为gather的函数来读取包含数据的文件,然后创建一个包含特定信息的新书,但是根本不会将它添加到数据库中。

我目前有一个控制器(bookController)和它的域。

我的域名非常简单:

class Book {

    static belongsTo = Author

    String toString() { bookNumber }

    Author bookAuthor
    String title


    static constraints = {
        bookAuthor()
        title()

    }
}

我只是'生成'了我的观点,所以我有基本的创建,编辑,列表和显示。我继续在控制器中添加了我自己的名为gather。对于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")
        }
    }
}

gsp出现但由于某种原因我的新书没有添加。当我添加println来测试信息是否在列表中时,它会显示具有正确信息的打印件。所以我很困惑为什么它不是“创建”新书实例并将其添加到数据库中。

有什么建议吗?

修改

作者的域类:

class Author {

    static hasMany = [books:Book]

    String authorName
    String notes

    String toString() { authorName }


    static constraints = {
        machineName()
        notes(maxSize:500)
    }
}

2 个答案:

答案 0 :(得分:2)

我建议您为控制器和/或域对象编写一些单元测试。使用此代码无法成功创建对象

new Book(Author:"$abc.author", Title:"$abc.title")

此处的return语句也没有意义

    numberOfBooks.times {
    def bookInstance = new Book(Author:"$abc.author", Title:"$abc.title")
        return [bookInstance: bookInstance]
    }

看起来你已经剪切并粘贴了代码,却没有理解代码在做什么。我想你想要更像这样的东西......

    // iterate through the list of books and create the object array to pass back
    def bookListInstance = []
    w.bookList.each {
        def bookInstance = new Book(Author:it.author, Title:it.title)
        bookListInstance << bookInstance
    }
    // now return the list of domain objects
    return [bookInstance: bookListInstance]

答案 1 :(得分:2)

你没有在任何Book实例上调用.save()......