20 Scaffolding - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.0.17
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
build.gradle
.dependencies { // ... compile "org.grails.plugins:scaffolding" // ... }
Dynamic Scaffolding
The simplest way to get started with scaffolding is to enable it by setting thescaffold
property in the controller to a specific domain class:class BookController { static scaffold = Book // Or any other domain class such as "Author", "Publisher" }
- index
- show
- edit
- delete
- create
- save
- update
http://localhost:8080/app/book
in a browser.Note: The old alternative of defining scaffold
property:class BookController { static scaffold = true }
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) } }
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] } }
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.
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
grails generate-views Book
grails generate-all Book
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() }
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) }
The Fields Plugin
The Grails scaffolding templates make use of the The Fields Plugin. Once you've generated the scaffold views, you can customize the forms and tables using the `Taglib` provided by the plugin (see the Fields plugin docs for details).<%-- Generate an HTML table from bookInstanceList, showing only 'title' and 'category' columns --%> <f:table collection="bookInstanceList" properties="['title', 'category']"/>