Go Web开发之Revel - Session/Flash
2013-01-06 09:55
330 查看
Revel提供两个基于cookie的存储机制.
它有下面的暗示:
大小不超过4kb
全部的数据必须被序列化为一个字符串存储
全部的数据可以被用户查看(它没有被编码), but it is safe from modification.
下面是这个模式的例子:
我们来看一下这个例子:
用户加载settings页面
用户post一个setting
应用程序处理这个request, 保存一个错误或成功信息到flash并重定向用户到setting页面
用户加载settings页面,模板将显示flash带来的的信息
它使用两个方便的函数:
Flash.Success(message string) 是 Flash.Out[“success”] = message 的缩写
至此完成.
// A signed cookie (and thus limited to 4kb in size). // Restriction: Keys may not have a colon in them. type Session map[string]string // Flash represents a cookie that gets overwritten on each request. // It allows data to be stored across one page at a time. // This is commonly used to implement success or error messages. // e.g. the Post/Redirect/Get pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get type Flash struct { Data, Out map[string]string }
Session
Revel session是一个字符串字典, 存储为加密签名的cookie.它有下面的暗示:
大小不超过4kb
全部的数据必须被序列化为一个字符串存储
全部的数据可以被用户查看(它没有被编码), but it is safe from modification.
Flash
Flash提供一个单次使用的字符串存储. 它对于实现the Post/Redirect/Get 模式很有帮助,或者用于转换"操作成功!"或"操作失败!"消息.下面是这个模式的例子:
// Show the Settings form func (c App) ShowSettings() rev.Result { return c.Render() } // Process a post func (c App) SaveSettings(setting string) rev.Result { c.Validation.Required(setting) if c.Validation.HasErrors() { c.Flash.Error("Settings invalid!") c.Validation.Keep() c.Params.Flash() return c.Redirect(App.ShowSettings) } saveSetting(setting) c.Flash.Success("Settings saved!") return c.Redirect(App.ShowSettings) }
我们来看一下这个例子:
用户加载settings页面
用户post一个setting
应用程序处理这个request, 保存一个错误或成功信息到flash并重定向用户到setting页面
用户加载settings页面,模板将显示flash带来的的信息
它使用两个方便的函数:
Flash.Success(message string) 是 Flash.Out[“success”] = message 的缩写
Flash.Error(message string)是 Flash.Out[“error”] = message 的缩写
至此完成.
相关文章推荐
- Go Web开发之Revel - 参数绑定
- 【Go web开发之revel+mgo】第6章 实现留言板功能
- Go Web开发之Revel - 概述
- Go Web开发之Revel - 路由
- 【Go web开发之revel+mgo】第7章 实现归档
- 【Go web开发之revel+mgo】第2章 Blog的首页与投稿设计
- Go Web开发之Revel - 返回值
- 【Go web开发之revel+mgo】第1章 述与环境
- 【Go web开发之revel+mgo】第5章 样式与留言板的设计
- Go Web开发之Revel - 介绍
- Go Web开发之Revel - 拦截器
- Go Web开发之Revel - 测试
- Go Web开发之Revel - app.conf
- 【Go web开发之revel+mgo】第11章 部署到heroku上
- Go Web开发之Revel - 插件
- Go Web开发之Revel - 部署
- Go Web开发之Revel - 概念
- Go Web开发之Revel - 验证
- Go Web开发之Revel - 日志
- 【Go web开发之revel+mgo】第3章 Blog的首页与投稿实现