Go Revel - Controllers(控制器)

时间:2023-03-10 06:20:33
Go Revel - Controllers(控制器)

Controller(控制器)整个revel都是围绕它处理所有请求

控制器可以是任何直接或间接内嵌了`*revel.Controller`类型的结构,如:

type AppController struct {
*revel.Controller
}

`*revel.Controller`必须位于结构的第一个字段,并且是匿名内嵌方式。

`revel.Controller`是一次请求的上下文,包含请求数据及返回数据。

type Controller struct {
Name string // controller名称
Type *ControllerType // controller的类型描述
MethodType *MethodType // 被调用的action的类型描述
AppController interface{} // controller实例

Request *Request // 请求体
Response *Response // 返回体
Result Result // server处理的数据

Flash Flash // flash信息,生命周期为一个请求
Session Session // 存储cookies
Params *Params // 请求的参数
Args map[string]interface{} // 每一个请求action传入的参数
RenderArgs map[string]interface{} // 传递给模板的参数
Validation *Validation // 数据验证器
}

// 请求参数
// 包含:
// - URL 请求URL
// - Form 表单值
// - File 上传的文件
type Params struct {
url.Values
Files map[string][]*multipart.FileHeader
}

// 请求体
type Request struct {
*http.Request
ContentType string
}

// server处理的返回体
type Response struct {
Status int
ContentType string
Headers http.Header
Cookies []*http.Cookie

Out http.ResponseWriter
}

对于每一个请求,Revel都会实例化一个对应的`Controller`,并为内嵌的`*revel.Controller`结构的相应字段赋值,因此,Revel并不会在不同的请求之间共享`Controller`

具体请移步:

Go Revel - server.go 源码分析 http://www.cnblogs.com/hangxin1940/p/3265538.html

Go Revel - Filter(过滤器) http://www.cnblogs.com/hangxin1940/p/3266311.html