通过Bootstrap.groovy将init数据添加到应用程序

时间:2014-01-14 20:29:31

标签: grails

我的域名图层如下:

@Resource(uri='/product')
class BasicProduct {

    String title
    String description
    Double price
    Date creationDate
    Date changedDate

    static constraints = {
        //everything is by default NotNull
        title(blank: false, unique: true)
        description(blank: false)
        price(blank: false, inList: [5,15,25,50,100])
        creationDate(min: new Date())
    }

}

我的Bootstrap.groovy包含该代码:

class BootStrap {

    def init = { servletContext ->
         new BasicProduct(title: "Product1", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:5).save()
         new BasicProduct(title: "Product2", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:75).save()
         new BasicProduct(title: "Product3", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:50).save()
         new BasicProduct(title: "Product4", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:25).save()
         new BasicProduct(title: "Product5", description:"blblblblbalablablalbalbablablablablblabalalbllba", price:15).save()

         println "initializing data..."
    }

但是,当我打开\product网址时,我看不到任何数据。

任何想法为什么?

感谢您的回答!

1 个答案:

答案 0 :(得分:5)

您可以这样做的日期:

Date dateCreated
Date lastUpdated 

static mapping = {
  autoTimestamp true
}