Last updated by admin 3 years ago
Scaffolding
??? Scaffolding?
Scaffolding?????????????????????:?? Scaffolding
??????scaffolding??????"scaffold"????"Book"????????????????(Controller)??"scaffold"???????true????:{excerpt:hidden=true}The simplest way to get started with scaffolding is to enable scaffolding via the "scaffold" property. For the "Book" domain class, you need to set the "scaffold" property on a controller to true:{excerpt}class BookController {
def scaffold = true
}def scaffold = Author.class
- list
- show
- edit
- delete
- create
- save
- update
??????java??????????hibernate??????????scaffolding????????import???????scaffold????????{excerpt:hidden=true}
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.{excerpt}?? Scaffolding
??Grails?????????????????????????scaffolding??????????????????scaffolding???????????????????????changeAuthor ??????????show??????{excerpt:hidden=true}Because Grails does not use code templates to achieve this 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:{excerpt}class BookController {
def scaffold = Book.class 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 {
def scaffold = Book.class // overrides scaffolded action to return both authors and books
def list = {
[ "books" : Book.list(), "authors": Author.list() ]
}
}????????
???scaffolding??????????????????????????????.Grails?????????????????????????????????????{excerpt:hidden=true}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:{excerpt}grails generate-controller
grails generate-views
grails generate-all
???????
Grails????????????????????????????????????????????????????{excerpt:hidden=true}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:{excerpt}def constraints = {
title()
releaseDate()
}def constraints = {
title()
category(inList:["Fiction", "Non-fiction", "Biography"])
releaseDate()
}def constraints = {
age(range:18..65)
}def constraints = {
name(length:0..30)
}def constraints = {
description(widget:'textarea')
}?Java???
?????????java??????hibernate????????grails?scaffolding??????????????????grails?hibernate??????????GORM?scaffold??hibernate????????????????{excerpt:hidden=true}If your domain model is written in Java and mapped with Hibernate you can still take advantage of Grails' scaffolding mechanism. For the purpose of this documentation it is assumed you have read how Grails' Hibernate integration works in the section on GORM, needless to say to scaffold a hibernate domain class simply reference the domain class in the controller:{excerpt}import com.books.HibernateBook
class BookController {
def scaffold = HibernateBook.class
}constraints = {
title(length:5..15)
desc(blank:false)
}


