////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


==== Creating a controller


Controllers can be created with the link:{commandLineRef}create-controller.html[create-controller] or link:{commandLineRef}generate-controller.html[generate-controller] command. For example try running the following command from the root of a Grails project:

[source,bash]
----
grails create-controller book
----

The command will create a controller at the location `grails-app/controllers/myapp/BookController.groovy`:

[source,groovy]
----
package myapp

class BookController {

    def index() { }
}
----

where "myapp" will be the name of your application, the default package name if one isn't specified.

`BookController` by default maps to the /book URI (relative to your application root).

NOTE: The `create-controller` and `generate-controller` commands are just for convenience and you can just as easily create controllers using your favorite text editor or IDE


==== Creating Actions


A controller can have multiple public action methods; each one maps to a URI:

[source,groovy]
----
class BookController {

    def list() {

        // do controller logic
        // create model

        return model
    }
}
----

This example maps to the `/book/list` URI by default thanks to the property being named `list`.


==== The Default Action


A controller has the concept of a default URI that maps to the root URI of the controller, for example `/book` for `BookController`. The action that is called when the default URI is requested is dictated by the following rules:

* If there is only one action, it's the default
* If you have an action named `index`, it's the default
* Alternatively you can set it explicitly with the `defaultAction` property:

[source,groovy]
----
static defaultAction = "list"
----
