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

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
主站蜘蛛池模板: 柯坪县| 连云港市| 平果县| 喜德县| 鄂托克旗| 深水埗区| 灵山县| 都江堰市| 江华| 永平县| 松桃| 多伦县| 临沭县| 东乌| 西平县| 孝感市| 扎赉特旗| 永修县| 陇南市| 鄂温| 拉孜县| 吴旗县| 秦安县| 灵台县| 兰西县| 台江县| 宜丰县| 宁晋县| 安康市| 麻栗坡县| 三门县| 两当县| 遂溪县| 昌平区| 贵南县| 北辰区| 南城县| 龙陵县| 宣武区| 舞钢市| 姜堰市|