From 06b873f2635c87ab6cbc57c43acccea4776b6cb8 Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 9 Jan 2013 18:27:07 +0800 Subject: [PATCH] =?UTF-8?q?session=E4=BE=8B=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 14.2.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/14.2.md b/14.2.md index 42656214..95d76f0c 100644 --- a/14.2.md +++ b/14.2.md @@ -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值操作