Java Reference
In-Depth Information
Figure 7-12.
Grails command history
Creating the Controller
A controller handles requests and creates or prepares responses. A controller can generate the
response directly or delegate to a view. To create a controller class, use the Grails
create-controller
target. This creates a new Grails controller class in the
grails-app/controllers
directory, as well as
the unit test for the controller class in
test/unit
. It also creates a
grails-app/views/<controller
name>
directory if it doesn't exist already.
To create the
BookController
class, you need to execute the
create-controller
target using an
optional class name, as shown here:
>grails create-controller book
If you don't supply the class name, you are prompted for one. The output of executing the
create-controller
target is shown here:
| Created file grails-app/controllers/bookstore/BookController.groovy
| Created file grails-app/views/book
| Created file test/unit/bookstore/BookControllerTests.groovy
Notice that when running the
create-controller
with the optional class name, you can leave the
class name in lowercase, and Grails will automatically uppercase it for you so that it follows the
standard Groovy class naming convention.