代码改变世界

Go Web开发之Revel - 概述

2013-01-03 10:43  Danny.tian  阅读(1667)  评论(0编辑  收藏  举报

一个Controller是任意嵌入rev.Controller的类型 (直接或间接嵌入)

典型的:

type AppController struct {
  *rev.Controller
}

目前,rev.Controller必须作为struct中的第一个元素

rev.Controller是请求的上下文.它包括请求和相应数据.详情请见the godoc,下面是Controller的定义

type Controller struct {
    Name          string          // The controller name, e.g. "Application"
    Type          *ControllerType // A description of the controller type.
    MethodType    *MethodType     // A description of the invoked action type.
    AppController interface{}     // The controller that was instantiated.

    Request  *Request
    Response *Response
    Result   Result

    Flash      Flash                  // User cookie, cleared after 1 request.
    Session    Session                // Session, stored in cookie, signed.
    Params     *Params                // Parameters from URL and form (including multipart).
    Args       map[string]interface{} // Per-request scratch space.
    RenderArgs map[string]interface{} // Args passed to the template.
    Validation *Validation            // Data validation helpers
    Txn        *sql.Tx                // Nil by default, but may be used by the app / plugins
}

// These provide a unified view of the request params.
// Includes:
// - URL query string
// - Form values
// - File uploads
type Params struct {
    url.Values
    Files map[string][]*multipart.FileHeader
}

type Request struct {
    *http.Request
    ContentType string
}

type Response struct {
    Status      int
    ContentType string
    Headers     http.Header
    Cookies     []*http.Cookie

    Out http.ResponseWriter
}

作为处理http请求的一部分,Revel实例化一个你的Controller的实例并在rev.Controller上面设置全部以上的属性,因此,Revel不在请求中共享Controller实例.