6.1.1 Understanding Controllers and Actions

h4. Creating a controller
h4. 创建控制器(Controllers)
Controllers can be created with the [create-controller|commandLine] target. For example try running the following command from the root of a Grails project:
控制器(Controllers)可以使用[create-controller|commandLine]目标来创建.作为示例可以在Grails根目录下尝试运行下列命令。
{code:java}
grails create-controller book
{code}

The command will result in the creation of a controller at the location @grails-app/controllers/BookController.groovy@:
这条命令将导致一个控制器(Controllers)在@grails-app/controllers/BookController.groovy@位置上被创建。
{code:java}
class BookController { ... }
{code}

@BookController@ by default maps to the /book URI (relative to your application root).
@BookController@默认映射到/book URI上(相对于您的应用程序根目录)
{note}
The @create-controller@ command is merely for convenience and you can just as easily create controllers using your favorite text editor or IDE
{note}
@create-controller@命令仅仅是个方面的工具,你还可以使用你喜欢的文本编辑器或者IDE来容易的创建控制器(Controllers)。
h4. Creating Actions
h4. 创建 Actions(操作)
A controller can have multiple properties that are each assigned a block of code. Each of these properties maps to a URI:
一个控制器(Controllers)可以拥有多个属性,每个属性都被分配一个代码块.上述每个这样的属性将被映射到URI上。
{code:java}
class BookController {
    def list = {

        // do controller logic
        // create model

        return model
    }
}
{code}

This example maps to the @/book/list@ URI by default thanks to the property being named @list@.
默认情况下这个例子映射到/book/list URI上,因为属性被命名为@list@。
h4. The Default Action
h4. 默认Action(操作)
A controller has the concept of a default URI that maps to the root URI of the controller. By default the default URI in this case is @/book@. The default URI is dictated by the following rules:
一个控制器(Controllers)具有默认URI的概念即映射到控制器(Controllers)的根URI。默认情况下缺省URI在这里的是@/book@。默认的URI被支配通过以下规则:
* If only one action is present the default URI for a controller maps to that action.
* 如果只有一个Action(操作)存在,一个控制器(Controllers)的默认URI将映射到该Action(操作)。
* If you define an @index@ action which is the action that handles requests when no action is specified in the URI @/book@
* 假如你定义了一个@index@ Action(操作),当没有Action(操作)被指定在URI@/book@上时,这个Action(操作)将处理请求。
* Alternatively you can set it explicitly with the @defaultAction@ property:
* 除此之外,你可以明确的设置为@defaultAction@属性:
{code:java}
def defaultAction = "list"
{code}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值