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: