在 Lift Scala 中存储会话变量

发布于 2024-08-07 12:40:58 字数 788 浏览 5 评论 0原文

我试图存储一个会话变量,然后用它来修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2024-08-14 12:40:58

为了从 SessionVarRequestVar 获取值,请调用其 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

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"?

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:

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
落花浅忆 2024-08-14 12:40:58

我认为脱节之处在于您假设 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 call setSessionAttribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文