2010-09-21 61 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