(Quick Reference)

20 Scaffolding - Reference Documentation

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

Version: 3.0.2

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

The way for an application to express a dependency on the scaffolding plugin is by including the following in build.gradle.

dependencies {

// ...

runtime "org.grails.plugins:scaffolding"

// ...

}

Static Scaffolding

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 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)
}

Customizing the Scaffolding templates

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