在 Lift Scala 中存储会话变量
我试图存储一个会话变量,然后用它来修改 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)调用对象,但它永远找不到它,所以我认为这可能有效,但我当我访问它时,即使实际的绑定和函数在菜单渲染之前执行,仍然会得到一个空框。
任何帮助将不胜感激。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了从
SessionVar
或RequestVar
获取值,请调用其is
方法,即sessionUserType.is
通过顺便问一下,您读过“管理状态”吗?
旁注
我相信
RequestVar
更适合您的情况。我不确定我是否可以在没有上下文的情况下正确捕获您的代码,但它至少可以重写为:In order to get a value from
SessionVar
orRequestVar
, callis
method on it, i.e.sessionUserType.is
By the way, did you read "Managing State"?
Side note
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:我认为脱节之处在于您假设 Session Attribute 将与您的 SessionVar 相匹配,但事实并非如此。 Lift 是一个非常安全的框架,如果它的功能是 Lift 创建的,那么它就是一个非常安全的框架不透明的 GUID 来引用组件
服务器端。
如果您希望
getSessionAttribute
返回某些内容,请考虑如何调用setSessionAttribute
。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 callsetSessionAttribute
.