Step by Step – creating a new webapp using Grails

These steps walk you through creating a new web app using the Grails framework.


Install Groovy and Grails as per instructions:

App Generation

Generate a new app from grails using grails create-app, and give it a name, say test_app

From the location where you ran the create-app target, an application directory with the name of your new application was created. cd into this directory, and create the entities for the application: grails create-domain-class and specify the entity name when prompted. Repeat for as many entities as you have in your model.

Database setup

To connect the app to a MySQL db, cd into grails-appconf (under your application directory), and edit the ApplicationDataSource.groovy file:

class ApplicationDataSource {
   @Property boolean pooled = true
   @Property String dbCreate = "create-drop" // one of 'create', 'create-drop','update'
   @Property String url = "jdbc:mysql://localhost/grails_test1"
   @Property String driverClassName = "com.mysql.jdbc.Driver"
   @Property String username = "grails"
   @Property String password = "grailspass"

Connect to your MySQL server with the command line mysql client, and create a new database:
create database grails_test1

Create the user and grant access to this new database:

grant all on grails_test1 to 'grails' identified by 'grailspass';
flush privileges;

Drop a copy of the MySQL JDBC connector jar into the lib directory under your application directory.

Generate scaffolding code

Generate the app: grails generate-all. Give the entity name when prompted, and repeat this step for each entity in the application.

Now start the test server: grails run-app

Bring up a browser and point it to http://localhost:8080/APPNAME – where APPNAME is the name of the application you just created.

You’ll be greeted by the application’s default index page, with a list of the Controllers, one per Entity in your application. Click on one and test the sample CRUD pages that were created.

Adding Attributes to your Entities

At this point your Entities only have default properties because we didn’t change them. To be of any use you’ll need to edit them and add additional properties to describe them.

Stop the webserver. I’ve found that modifying the entities while the app is running does not automatically update your tables in your database.

Under your app directory, go into grails-appdomain. You’ll find the source for your Entities here. Assuming you have an Enity called Category, lets add a couple of properties:

class Category { 
	@Property Long id
	@Property Long version

    String toString() { "${} :  $id" }


Underneath the version property, add a name and description properties:

	@Property String name
	@Property String description

Now rebuild with grails generate-all, then restart your app with grails run-app.

At this point go back to your MySQL client and describe your tables to see their structure (desc table_name). Notice that due to the ‘create-drop’ property in your datasource config that we configured earlier, when you start your app Grails is automatically creating your tables for you, based on your Entities (this behavior is configurable).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.