session例子

This commit is contained in:
astaxie
2013-01-09 18:27:07 +08:00
parent c08c7c6175
commit 06b873f263

22
14.2.md
View File

@@ -55,6 +55,28 @@ beego中主要有如下这些全局变量来控制session处理
## session使用
通过上面的代码我们可以看到beego框架很方便的就继承了session功能那么我们在项目中如何使用呢请看的示例代码
func (this *MainController) Get() {
var intcount int
sess := this.StartSession()
count := sess.Get("count")
if count == nil {
intcount = 0
} else {
intcount = count.(int)
}
intcount = intcount + 1
sess.Set("count", intcount)
this.Data["Username"] = "astaxie"
this.Data["Email"] = "astaxie@gmail.com"
this.Data["Count"] = intcount
this.TplNames = "index.tpl"
}
上面的代码展示了如何在我们自己的控制逻辑中使用session主要分两个步骤
1. 获取session对象
2. 使用session进行一般的session值操作