在Lift Scala中存储会话变量

时间:2009-10-10 01:49:07

标签: session scala variables lift

我正在尝试存储会话变量,然后使用它来修改Boot.scala中的菜单。以下是我将变量存储在代码段中的方法:

object sessionUserType extends  SessionVar[String](null)
  def list (xhtml : NodeSeq) : NodeSeq = {

    Helpers.bind("sendTo", xhtml, 
                 "provider" -> SHtml.link("/providerlogin",() => sessionUserType("provider"), Text("Provider")),
                 "student" -> SHtml.link("/studentlogin",() => sessionUserType("student"), Text("Student")))
    }

然后在Boot.scala中我这样做:

val studentSessionType = If(() => S.getSessionAttribute("sessionUserType").open_!.equals("student"),
            "not a student session") 

我也尝试按名称调用对象(sessionUserType),但它永远找不到它,所以我认为这可能有效,但是当我访问它时我仍然得到一个空盒子,即使实际的绑定和函数被执行在菜单渲染之前。

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:10)

要从SessionVarRequestVar获取值,请在其上调用is方法,即sessionUserType.is

顺便问一下,你读过“Managing State”吗?

旁注

我相信RequestVar更适合您的情况。我不确定如果没有上下文我可以正确捕获你的代码,但它至少可以重写为:

case class LoginType
case object StudentLogin extends LoginType
case object ProviderLogin extends LoginType

object loginType extends RequestVar[Box[LoginType]](Empty)
// RequestVar is a storage with request-level scope

...
"provider" -> SHtml.link("/providerlogin",() => loginType(Full(ProviderLogin)), Text("Provider")),
// `SHtml.link` works in this way. Your closure will be called after a user
// transition, that is when /providerlogin is loading.
...

val studentSessionType = If(() => { loginType.is map {_ == StudentLogin} openOr false },
                "not a student session")
// As a result, this test will be true if our RequestVar holds StudentLogin,
// but it will be so for one page only (/studentlogin I guess). If you want
// scope to be session-wide, use SessionVar

答案 1 :(得分:0)

我认为断开连接是假设a 会话属性将匹配您的 SessionVar ,而事实并非如此。 Lift是一个非常secure的框架,如果它的功能是Lift会创建不透明的GUID来引用它上面的组件。 服务器端。

如果您希望getSessionAttribute返回某些内容,请考虑如何致电setSessionAttribute

相关问题