Java Reference
In-Depth Information
Listing 7-19. The List Action of the BookController
def list(Integer max) {
params.max = Math.min(max ?: 10, 100)
[bookInstanceList: Book.list(params), bookInstanceTotal: Book.count()]
}
The first line of the list closure is working with the
params
property, which is a Map containing all the
parameters of the incoming request.
The last line returns a Map with two elements:
bookInstanceList
and
bookInstanceTotal
. The
bookInstanceList
is loaded with a call to
Book.list( )
. The
list( )
is being passed the
params
Map, from which it will pull any parameters that it can use. The
bookInstanceTotal
is loaded with
Book.count()
.The use of the
bookInstanceTotal
will be mentioned in the “List View” section later.
The
list()
action renders the list view using the data in the Map that's returned from this action.
Create Action
Listing 7-20 illustrates the
create()
action of the
BookController
.
Listing 7-20. The Create Action of the BookController
def create() {
[bookInstance: new Book(params)]
}
The
create()
action creates a new
Book
instance and then assigns the params to
bookInstance
's
property because it will be used later, as explained in the “Save Action” section discussed next in
Listing 7-21. Then it returns that instance in a Map with the key of
bookInstance
. Finally, it renders
the create view.
Save Action
Listing 7-21 illustrates the
save()
action of the
BookController
.
Listing 7-21. The Save Action of the BookController
def save() {
def bookInstance = new Book(params)
if (!bookInstance.save(flush: true)) {
render(view: "create", model: [bookInstance: bookInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'book.label',
default: 'Book'), bookInstance.id])
redirect(action: "show", id: bookInstance.id)
}