Tutorial‎ > ‎

Building Logic Tutorial - Grails

Grails 2.0.3

This tutorial has been tested with Grails 2.0.3 (current as of this writing).  Verify that is printed out in the console log.
You can use this Tutorial to understand how to add business logic to a Java-based web project, from scratch.  The project can be completed in under 20 minutes.

This is Part I, where we create a project with Domain Objects, sample data, and a default User Interface.  Completing this (requires about 7 minutes) prepares you for Part II, where we add business logic to the project created here.

This tutorial introduces the following key concepts: 
  • @Logic - how to define Logic Classes and Logic Annotations
  • Complex multi-table logic - how your logic addresses complex, multi-table logic
  • Automated Dependency Management - how logic dependencies are detected and automated
  • Object Declarative - how you can use Java/Groovy Logic Methods for complexity and extensibility
  • Assured Re-use / Integrity - how the Business Logic Engine ensures your logic is re-used
  • Logic Debugging - how to verify your logic is operating properly


In this Tutorial, we will build a small Customer, PurchaseOrder, LineItem and Product application from scratch.  The bottom of this page includes several zip files provided to save typing.

Our focus is on the business logic, not the User Interface, persistence or IDE.  Our business logic objectives, captured on the obligatory cocktail napkin:


The requirements are enforced over a series of Use Cases that touch this data:
  • Adding and changing new PurchaseOrder and their LineItems

  • Making an PurchaseOrder ready, or paid

  • Reassigning a PurchaseOrder to a different Customer

  • Changing LineItem quantities, or reassigning to different Products

Of course, a Data Model and User Interface is required for logic execution.  For these, we will utilize Spring Grails since it provides default services for those elements, using Groovy.  You can perform a similar tutorial in a wide variety of other IDEs and frameworks.


You will need SpringSource Tool Suite (STS), as explained here.  STS is an extended version of Eclipse, which includes Grails (and Roo, a similar productivity environment built around Groovy; ABL supports both frameworks, and a number of others).

This tutorial presumes a basic understanding of Hibernate/JPA and Java.  It does not presume a background in STS or Grails.

Create Project

Execute the steps below to create a new project, its domain model, sample data, and a user interface.  This is standard Grails functionality, preparing a basic system to which we will then add business logic.

Create Empty Folder (STS Workspace) and start STS

Create a new empty folder in your file system, and open STS specifying that as your workspace.  Alter preferences as desired (it is usually a good idea to set Preferences > Run/Debug > Console to unlimited).

Create Project abltutorial

To create a new Grails Project:
  1. Click the upper left "New" button to create a new Grails Project (New > Grails Project)
  2. Specify the project name abltutorial

You now have an empty project.  Grails has defaulted the persistence setup to an in-memory database.  This suits our purposes fine, since we want to reload test data each time we test.

Import Artifacts

You are now ready to use Grails services to create Domain Classes, User Interfaces (Controllers and Views), and populate test data.  Since we're focused on business logic, we'll speed things up by importing these.

 Windows Mac
Since the Navigator Paste menu is not enabled, you use the Windows Explorer, as follows:
  • In Windows Explorer
    1. Open to your projects' grails-app folder.
    2. Paste the unzipped grails-app.zip contents (use the link above) into that folder
      • be sure you choose the right folder level - you should be replacing files)
  • In STS
    1. Right-click the abltutorial project and click Refresh
    2. Finally, edit the conf/Bootstrap file and save it - just add a comment or space.
      • This is required to alert Grails to execute the Bootstrap to load your test data
Use this procedure on the Mac (see the screen shot below):
  1. Open Navigator View (Window > Show View > Other > Navigator)
    • Do not perform this procedure in Grails Project view - the files will not be copied to the correct locations
  2. Download and unpack grails-app.zip
  3. Paste the downloaded grails-app folder into the abltutorial project as shown below (replace existing contents)
    • be sure you choose the right folder level - you should be replacing files)
  4. Return to Grails Project View

Import Verification

Please perform the Import Verification, which will explain what was imported, and verify that the import is correct.

Test User Interface

Your application is now ready to run.  Start the server as shown below:

When the server starts, the console will provide a URL to start the app (note cursor position below) - click it to start your app.  You can, for example, click abltutorial.CustomerController to explore your data:

No Data?

Be sure you open, edit and save the conf/Bootstrap.groovy file so Grails recognizes it.

Project Creation Complete - Part 1

You now have a simple, but complete, Grails project, including a database schema and a web interface.

Well, maybe not that complete!  The business logic - see our cocktail napkin above - is totally missing.  This is first apparent from the loaded data (all the Customer balances are 0), and continues to be missing if you create, update or delete orders.

Not to fault Grails - frameworks simply aren't built to address this element of your app.  The problem is, this is about half the app (even more as framework automation increases).

Fortunately, ABL is built to address this part of your application.  So, let's get on with Part II and introduce some business logic.

Val Huber,
Jun 4, 2012, 3:31 PM