Go Revel - Parameters(参数绑定)

参数绑定

Revel框架会尽可能的将提交参数转换为期望的Go类型。这个从一个字符串提交参数转换为另一个类型被称为数据绑定

参数

所有的请求参数被存放在一个Params中,他包括:

URL路径参数
URL查询参数
表单值(或Multipart)
文件上传

这是Params的定义:

type Params struct {
    url.Values
    Files map[string][]*multipart.FileHeader
}

嵌入的url.Values提供了对简单值的访问,但开发者会发现非字符的值使用revel的数据绑定机制也非常容易。

Action参数

Params中的参数可以当作方法参数被Action接收。例如:

func (c AppController) Action(name string, ids []int, user User, img []byte) revel.Result {
    ...
}

在调用之前,revel会使用Binder绑定器将提交的参数按名称转换为期望类型,如果转换不成功,将会用期望类型的0值代替。

Binder绑定器

绑定一个提交参数至期望的类型。

func (c SomeController) Action() revel.Result {
    var ids []int
    c.Params.Bind(&ids, "ids")
    ...
}

以下的数据类型支持开箱转换:

任意宽度的int整数
Bool类型
任意支持类型的指针
任意支持类型的切片
结构
time.Time 类型的日期与时间
*os.File, []byte, io.Reader, io.ReadSeeker 文件上传用到的

Booleans

字符串值true, on, 与 1 都会被转为true,否则被转为false

Slices

对于slices切片有两种支持: ordered 有序 与 unordered 无序.

有序:

?ids[0]=1
&ids[1]=2
&ids[3]=4

slice中的数据 []int{1, 2, 0, 4}

无序:

?ids[]=1
&ids[]=2
&ids[]=3

slice中的数据 []int{1, 2, 3}

只有struct切片将会使用有序切片

?user[0].Id=1
&user[0].Name=rob
&user[1].Id=2
&user[1].Name=jenny

Structs

struct使用.符号来进行绑定

?user.Id=1
&user.Name=rob
&user.Friends[]=2
&user.Friends[]=3
&user.Father.Id=5
&user.Father.Name=Hermes

将会绑定为:

type User struct {
    Id int
    Name string
    Friends []int
    Father User
}

只有可导出字段才能被绑定。

日期与时间

revel内置了SQL标准时间字符串格式[“2006-01-02”, “2006-01-02 15:04”]

可以用如下方法添加其他的时间格式:

func init() {
    revel.TimeFormats = append(revel.TimeFormats, "01/02/2006")
}

文件上传

上传的文件可以被绑定为以下任意一种类型:

*os.File
[]byte
io.Reader
io.ReadSeeker

上传使用Go的multipart来处理。上传的数据首先被保存在内存中,当大小超过10MB(默认)时,会保存至临时文件。

当绑定为os.File类型时,revel会默认将上传的文件存储至临时文件,这样相比其他类型,效率比较低。

自定义 Binders

创建自定义Binder绑定器,只需要实现binder接口并且注册。

func myBinder(params Params, name string, typ reflect.Type) reflect.Value {
    ...
}

func init() {
    revel.TypeBinders[reflect.TypeOf(MyType{})] = myBinder
}

posted on   黑暗伯爵  阅读(1978)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示