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

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
主站蜘蛛池模板: 湾仔区| 天峨县| 鲜城| 丰城市| 河南省| 宜兴市| 福贡县| 黄梅县| 恩施市| 黄陵县| 吉木乃县| 万年县| 莱阳市| 济宁市| 平罗县| 葫芦岛市| 乐安县| 通江县| 甘德县| 五大连池市| 通化市| 浦江县| 翁源县| 米脂县| 千阳县| 景宁| 博湖县| 佛山市| 临武县| 宜川县| 麻栗坡县| 天水市| 海原县| 徐水县| 盐源县| 安庆市| 扎赉特旗| 长垣县| 高平市| 靖西县| 耒阳市|