(Quick Reference)

20 Scaffolding - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari

Version: 2.5.6

20 Scaffolding

Scaffolding lets you generate some basic CRUD interfaces for a domain class, including:
  • The necessary views
  • Controller actions for create/read/update/delete (CRUD) operations

As of Grails 2.3, the scaffolding feature has been moved to a plugin. By default this is configured for installation in new applications, but if you are upgrading from a previous version of Grails you will need to add the following configuration to your BuildConfig.groovy file:

plugins {
        …
        compile ":scaffolding:2.0.0"
        …
    }

Version 1.0.0 of the plugin provides the same scaffolding seen in Grails 2.2.x and below. Version 2.0.x of the scaffolding plugin includes different scaffolding templates that are aligned with the new REST APIs introduced in Grails 2.3 and above.

Dynamic Scaffolding

The simplest way to get started with scaffolding is to enable it with the scaffold property. Set the scaffold property in the controller to true for the Book domain class:

class BookController {
    static scaffold = true
}

This works because the BookController follows the same naming convention as the Book domain class. To scaffold a specific domain class we could reference the class directly in the scaffold property:

class SomeController {
    static scaffold = Author
}

With this configured, when you start your application the actions and views will be auto-generated at runtime. The following actions are dynamically implemented by default by the runtime scaffolding mechanism:

  • index
  • show
  • edit
  • delete
  • create
  • save
  • update

A CRUD interface will also be generated. To access this open http://localhost:8080/app/book in a browser.

If you prefer to keep your domain model in Java and mapped with Hibernate you can still use scaffolding, simply import the domain class and set its name as the scaffold argument.

You can add new actions to a scaffolded controller, for example:

class BookController {

static scaffold = Book

def changeAuthor() { def b = Book.get(params.id) b.author = Author.get(params["author.id"]) b.save()

// redirect to a scaffolded action redirect(action:show) } }

You can also override the scaffolded actions:

class BookController {

static scaffold = Book

// overrides scaffolded action to return both authors and books def index() { [bookInstanceList: Book.list(), bookInstanceTotal: Book.count(), authorInstanceList: Author.list()] }

def show() { def book = Book.get(params.id) log.error(book) [bookInstance : book] } }

All of this is what is known as "dynamic scaffolding" where the CRUD interface is generated dynamically at runtime.

By default, the size of text areas in scaffolded views is defined in the CSS, so adding 'rows' and 'cols' attributes will have no effect.

Also, the standard scaffold views expect model variables of the form <propertyName>InstanceList for collections and <propertyName>Instance for single instances. It's tempting to use properties like 'books' and 'book', but those won't work.

Customizing the Generated Views

The views adapt to Validation constraints. For example you can change the order that fields appear in the views simply by re-ordering the constraints in the builder:

def constraints = {
    title()
    releaseDate()
}

You can also get the generator to generate lists instead of text inputs if you use the inList constraint:

def constraints = {
    title()
    category(inList: ["Fiction", "Non-fiction", "Biography"])
    releaseDate()
}

Or if you use the range constraint on a number:

def constraints = {
    age(range:18..65)
}

Restricting the size with a constraint also effects how many characters can be entered in the generated view:

def constraints = {
    name(size:0..30)
}

Static Scaffolding

Grails also supports "static" scaffolding.

The above scaffolding features are useful but in real world situations it's likely that you will want to customize the logic and views. Grails lets you generate a controller and the views used to create the above interface from the command line. To generate a controller type:

grails generate-controller Book

or to generate the views:

grails generate-views Book

or to generate everything:

grails generate-all Book

If you have a domain class in a package or are generating from a Hibernate mapped class remember to include the fully qualified package name:

grails generate-all com.bookstore.Book

Customizing the Scaffolding templates

The templates used by Grails to generate the controller and views can be customized by installing the templates with the install-templates command.