Java Reference
In-Depth Information
Returning a Model
The third option to exit a controller action is either to return a model that is a Map containing data, as
illustrated in Listing 7-17.
Listing 7-17. Returning the Model
class HelloController {
def show() {
[user: User.get(params.id)]
}
}
Grails will attempt to render a view with the same name as the action. It will look for this view in
a directory named after the base name of the controller. In Listing 7-17, returning from the show()
action of the HelloController will cause Grails to render the view /views/hello/show.gsp .
Now you know how the action of the controller can be called and how it can be exited. Fortified with
this knowledge, take a look at Figure 7-28 and let's start investigating each action one by one. First
let's get the allowedMethods property out of the way.
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
The allowedMethods property provides a simple declarative syntax to specify which HTTP methods
are allowed for your controller actions. By default, all request methods are allowed for all controller
actions. The allowedMethods property is optional and needs to be defined only if the controller has
actions that need to be restricted to certain request methods. This property in the BookController
specifies that only save, update, and delete can be POST methods.
Index Action
The index() action is the default action that is called when you navigate to the BookController . By
default, this action just redirects to the list() action using the redirect() method explained earlier
as illustrated in Listing 7-18.
Listing 7-18. The Index Action of the BookController
def index() {
redirect(action: "list", params: params)
}
The redirect() method issues an HTTP redirect to a URL constructed from these parameters. If the
action is not specified, the index() action will be used. The params hold request parameters, if any.
List Action
Listing 7-19 illustrates the list() action of the BookController .
 
Search WWH ::




Custom Search