Reputation:
I am trying to store a session variable and then use it to modify the menu in Boot.scala. Here is how I am storing the variable in a snippet:
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")))
}
Then in Boot.scala I do this:
val studentSessionType = If(() => S.getSessionAttribute("sessionUserType").open_!.equals("student"),
"not a student session")
I also tried to call the object by name (sessionUserType), but it can never find it, so I thought this might work, but I keep getting an empty box when i access it even though the actual binding and function get executed prior to the menu rendering.
Any help would be much appreciated.
Thanks
Upvotes: 8
Views: 5329
Reputation: 13221
In order to get a value from SessionVar
or RequestVar
, call is
method on it, i.e. sessionUserType.is
By the way, did you read "Managing State"?
I believe RequestVar
is a better fit in your case. I am not sure I could catch your code right without the context, but it could be rewritten at least like:
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
Upvotes: 11
Reputation: 4008
I think the disconnect is that you are assuming that the a Session Attribute is going to match your SessionVar, and that is not the case. Lift is a very secure framework, and one if its features is that Lift creates opaque GUIDs to refer to components on the server side.
If you want getSessionAttribute
to return something, think about how you can call setSessionAttribute
.
Upvotes: 0