2010-09-21 63 views
0

好吧我之前问过一个问题,但对此不太确定。所以我继续等到现在再问一次。添加实例的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) 
    } 
} 

回答

2

你没有任何书实例调用.save()...

+0

谢谢你指出,但这是我第一次尝试,并没有改变结果。 – StartingGroovy 2010-09-23 20:12:17

+0

那么除非你使用这种方法,否则他们绝对不会保存。你一定还有其他的错误。检查save()的返回值是否为空。如果是这样,有一个验证错误阻止他们坚持 – leebutts 2010-09-24 06:20:44

+0

谢谢,我会检查并回发。 – StartingGroovy 2010-09-24 18:49:31

2

我会suggets你控制器和/或域对象编写一些单元测试。不能被这种代码

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] 
+0

我继续改变了我的收集行动,复制什么你在上面用我的_athor = new Author(“$ abc.author”)做了它,但它仍然没有添加它们。它实际上仍然产生了和以前相同的信息。另外,为什么你希望在保存操作中看到这一点? (我刚刚把它生成了) – StartingGroovy 2010-09-21 23:32:26

+0

我说了一些“like”!,你没有在这里提供足够的代码来获得完整的解决方案......你没有展示Author对象的样子?你还没有显示什么例外,如果有的话? – 2010-09-22 00:08:32

+0

没有例外。我将发布Author对象的外观。我的歉意Aaron,我之前没有处理过生成的视图,我正在考虑做这样的事情与我读过的教程非常相似。我并没有质疑你的工作,只是想知道为什么你可能会这样做:)首先,我创建了一个没有db的grails应用程序,现在我想要实现它,并遇到上述情况。 *在原始问题* – StartingGroovy 2010-09-22 00:21:48