官术网_书友最值得收藏!

Bootstrapping demo data

You saw earlier that Grails comes pre-configured with an in-memory HSQLDB database. The benefit of this is that you don't need to configure a database server to get started. The problem is that whenever you restart Grails, all your data is lost. Grails provides a handy notion of Bootstrapping data, so a set of demo data can be loaded into your application during startup. At the very least, you will want to add the User and Administrator roles to this bootstrap to save you re-creating them each time.

The BootStrap class can be found under the grails-app/conf folder. Update the init code block to create and save the two new roles:

import app.Role 
class BootStrap {
def init = { servletContext ->
def user = new Role(name: 'User').save() def admin = new Role(name: 'Administrator').save() 
}
def destroy = {
}
}

The BootStrap class is able to interact with the ServletContext of the application as it is passed as an argument to the init closure.

Now that the roles have been pre-configured for the application, it is possible to create users that belong to one of these roles as shown in the following screenshot:

Bootstrapping demo data
主站蜘蛛池模板: 横峰县| 岳阳市| 桂东县| 栾城县| 石狮市| 井陉县| 枞阳县| 乐平市| 雷州市| 萝北县| 林周县| 石家庄市| 三明市| 文登市| 济南市| 中卫市| 台安县| 台湾省| 石狮市| 商水县| 莆田市| 二手房| 曲麻莱县| 南华县| 双辽市| 平顶山市| 通辽市| 兴义市| 库伦旗| 荣成市| 恩平市| 桐城市| 栖霞市| 平山县| 郴州市| 清河县| 常州市| 阿拉善盟| 邢台市| 澄城县| 元氏县|