8 variables session scala 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")))
}
Run Code Online (Sandbox Code Playgroud)
然后在Boot.scala中我这样做:
val studentSessionType = If(() => S.getSessionAttribute("sessionUserType").open_!.equals("student"),
"not a student session")
Run Code Online (Sandbox Code Playgroud)
我也尝试通过名称调用对象(sessionUserType),但它永远找不到它,所以我认为这可能有用,但是当我访问它时我仍然得到一个空盒子,即使实际的绑定和函数在执行之前执行菜单渲染.
任何帮助将非常感激.
谢谢
Ale*_*rov 10
为了从中获取值,SessionVar
或者RequestVar
调用is
方法,即sessionUserType.is
顺便问一下,你读过" 管理国家 "吗?
我认为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
Run Code Online (Sandbox Code Playgroud)