beego中路由(Router)參數和表單(Form)參數的區別和獲取

在beego中,視圖層有兩種叫做參數的東西,有時候很讓人困惑。它們分別是路由參數表單參數

beego的路由映射支持靈活的結構,比如對於這種/blog/:catName可以表示的是某一個分類下的blog列表,那麼這裏的:catName就是路由參數;如果說我們要對這個分類下面的blog進行分頁,想查看第10頁的blog,那麼我們的url可能變成了/blog/:catName?page=10這種格式,那麼這裏的page就是表單參數。表單參數既可以是GET類型的參數也可以是POST類型的參數,總之都叫做表單參數。

1. 獲取路由參數的方法

可以使用下面的方法來獲取路由參數:

方法 原型
GetInt func (c *Controller) GetInt(key string) (int64, error)
GetBool func (c *Controller) GetBool(key string) (bool, error)
GetFloat func (c *Controller) GetFloat(key string) (float64, error)
GetString func (c *Controller) GetString(key string) string

我們爲了演示這些方法構建了一個路徑:

beego.Router("/blog/:catId/:catName:/:catPublish:/:catPrice", &controllers.MainController{}, "get:Blog")

然後我們可以用下面的方法獲取路徑參數:

func (this *MainController) Blog() {
    catId, _ := this.GetInt(":catId")
    catName := this.GetString(":catName")
    catPublish, _ := this.GetBool(":catPublish")
    catPrice, _ := this.GetFloat(":catPrice")
    beego.Debug(fmt.Sprintf("Category Id:%d Name:%s Publish:%v Price:%f", catId, catName, catPublish, catPrice))
}

然後訪問http://localhost:8080/blog/30/beego/true/98.45,可以得到下面的輸出:

2014/09/02 14:25:04 [D] Category Id:30 Name:beego Publish:true Price:98.450000
2014/09/02 14:25:04 [C] the request url is /blog/30/beego/true/98.45

其實我們可以去看看這些Get方法,比如GetString(github.com/astaxie/beego/controller.go 367行):

func (c *Controller) GetString(key string) string {
    return c.Ctx.Input.Query(key)
}

從上面的代碼可以看到實際上這些路徑參數都是從c.Ctx.Input裏面獲取的,而這個參數類型是Context,定義在github.com/astaxie/beego/context/context.go裏面。

type Context struct {
    Input          *BeegoInput
    Output         *BeegoOutput
    Request        *http.Request
    ResponseWriter http.ResponseWriter
    _xsrf_token    string
}

然後我們再看看Context裏面的Input,這是一個*BeegoInput類型(定義在github.com/astaxie/beego/context/input.go)裏面:

type BeegoInput struct {
    CruSession    session.SessionStore
    Params        map[string]string
    Data          map[interface{}]interface{} // store some values in this context when calling context in filter or controller.
    Request       *http.Request
    RequestBody   []byte
    RunController reflect.Type
    RunMethod     string
}

然後我們再看這個結構體的Query方法的定義:

// Query returns input data item string by a given string.
func (input *BeegoInput) Query(key string) string {
    if val := input.Param(key); val != "" {
        return val
    }
    if input.Request.Form == nil {
        input.Request.ParseForm()
    }
    return input.Request.Form.Get(key)
}

我們驚奇地發現這個Query方法並不單純,它同時支持查詢路由參數和表單參數。所以一般情況下你也可以用來查詢表單參數。

2. 獲取表單參數的方法
上面我們看過了獲取路由參數的方法,這裏我們再看一下獲取表單參數的方法。在上面的獲取路由參數的講解最後,我們發現可以使用和上面相同的方法來獲取表單參數。

方法 原型
GetInt func (c *Controller) GetInt(key string) (int64, error)
GetBool func (c *Controller) GetBool(key string) (bool, error)
GetFloat func (c *Controller) GetFloat(key string) (float64, error)
GetString func (c *Controller) GetString(key string) string

驗證很簡單,使用這樣的url:http://localhost:8080/blog/30/beego/true/98.45?page=10 和代碼:

page, _ := this.GetInt("page")
beego.Debug("Page", page)

輸出:

2014/09/02 14:41:07 [D] Page 10

當然除了上面的方法之外,還有兩個方法可以用來獲取表單參數:

名稱 原型 描述
GetStrings func (c *Controller) GetStrings(key string) []string 用來獲取比如多選框的值
GetFile func (c *Controller) GetFile(key string) (multipart.File, *multipart.FileHeader, error) 用來獲取上傳的文件

好了,其實一般情況下,獲取路由參數和表單參數不會對用戶如此透明,直接用GetXXX方法獲取就可以了。

作者 jemygraw • 2014-09-02 •

http://golanghome.com/post/327

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章