16 Scaffolding - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: 1.3.9
16 Scaffolding
Scaffolding allows you to auto-generate a whole application for a given domain class including:- The necessary views
- Controller actions for create/read/update/delete (CRUD) operations
Enabling Scaffolding
The simplest way to get started with scaffolding is to enable scaffolding via thescaffold
property. For the Book
domain class, you need to set the scaffold
property on a controller to true:class BookController { static scaffold = true }
BookController
follows the same naming convention as the Book
domain class. If we wanted to scaffold a specific domain class we could reference the class directly in the scaffold property:class SomeController {
static scaffold = Author
}
- list
- show
- edit
- delete
- create
- save
- update
http://localhost:8080/app/book
If you prefer to keep your domain model in Java and mapped with Hibernate you can still use scaffolding, simply import the necessary class and set the scaffold property to it.Dynamic Scaffolding
Note that when using the scaffold property Grails does not use code templates, or code generation to achieve this so you can add your own actions to the scaffolded controller that interact with the scaffolded actions. For example, in the below example,changeAuthor
redirects to the show
action which doesn't actually exist physically: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) } }
class BookController { static scaffold = Book // overrides scaffolded action to return both authors and books def list = { [ bookInstanceList : Book.list(), bookInstanceTotal: Book.count(), authorInstanceList: Author.list() ] } def show = { def book = Book.get(params.id) log.error(book) [ bookInstance : book ] }}
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 that Grails generates have some form of intelligence in that they adapt to the 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() }
inList
constraint:def constraints = { title() category(inList:["Fiction", "Non-fiction", "Biography"]) releaseDate() }
range
constraint on a number:def constraints = { age(range:18..65) }
def constraints = { name(size:0..30) }
Generating Controllers & Views
The above scaffolding features are useful but in real world situations its likely that you will want to customize the logic and views. Grails allows you to generate a controller and the views used to create the above interface via the command line. To generate a controller type:grails generate-controller Book
grails generate-views Book
grails generate-all Book
grails generate-all com.bookstore.Book