Creating Controllers and Actions
Creating Grails Controllers
If you're lazy you can create a controller using the "create-controller" target which will prompt for the name of your controller:
For example if we typed "book" the following controller would be created:
class BookController { … }
BookController maps to the <...>/book URI. Note that grails has done no special configuration in the background, all this is doing is creating you a controller from a template.
Creating Actions
A controller can have multiple closure properties. Each of these properties maps to a URI:
class BookController {
def list = { // do controller logic
// create model return model
};
}
This example maps to the <...>/book/list URI. If only one closure property is present the default URI for a controller maps to this property. Alternatively you can define an "index" action which is the action that handles requests when no action is specified in the URI i.e. <...>/book
Controller actions may accept command object parameters. See the
Command Object documentation for details.
Setting the default action
There are 2 ways to set the default action (ie the action that is executed if no only the controller name is included in the uri), the simplest way is to merely create an action called "index":
def index = {
redirect(action:list)
}
Alternatively you can set it explicitly with the "defaultAction" property:
def defaultAction = "list"
Restricting Access To Actions
By default all controller actions are accessible using any HTTP request method (GET, PUT, POST, etc...). The documentation for
HTTP Method Restrictions explains how to restrict access to certain actions.