gpt4 book ai didi

session - 我的 SessionVar 对象应该在哪里?

转载 作者:行者123 更新时间:2023-12-02 15:27:19 24 4
gpt4 key购买 nike

Lift (Scala) 中编程确实压力很大,它们的文档都非常匮乏,而且您能找到的文档也不完整且具有误导性。

嗯,我想做的是将一个简单的字符串存储在 SessionVar 中。因此,一个片段将使用表单填充该字符串的值,而在另一个片段中我将在 session 中显示该字符串(或其默认值)。

到目前为止我所拥有的是:

SessionVar 对象:

// the SessionVar will contain a String with "Anonymous" as default value.
object myUser extends SessionVar[String]("Anonymous")

我填充字符串的片段:

object Login extends LiftScreen {

val name = field("Name: ", "")

def finish() {
// set the SessionVar string with the string entered
myUser.set(name)
S.notice("Your name is: "+name)
}
}

我显示字符串的片段(另一个片段):

// show the string in SessionVar
"Your name: " + myUser.is
...

MyUser 是我在 session 中保存的对象。最大的问题是:我在哪里保存我的 MyUser 对象?我在 Boot.scala 和两个片段中尝试过,但我不断收到此错误:not find: value myUser

我应该把它放在哪里?我应该如何导入它?我怎样才能让它发挥作用?

最佳答案

您可以将 SessionVar 放置在与 LiftScreen 相同的"file"上,但位于对象定义之外。

类似这样的事情:

package com.code.snippet
import ...

object myUser extends SessionVar[String]("Anonymous")
object Login extends LiftScreen {

val name = field("Name: ", "")

def finish() {
// set the SessionVar string with the string entered
myUser.set(name)
S.notice("Your name is: "+name)
}
}

现在,在您的另一个片段上,假设您将其放在不同的文件中(我认为这是因为您正在使用 LiftScreen,但如果您使用的是常规片段类,您可能有多个方法来渲染用户界面。在另一个文件上,您确实需要导入对象。

package com.code.snippet
import com.code.snippet.myUser
class MySnippet {
render ={
"#message" #> "Your name: " + myUser.is
}
}

你也可以这样做:

package com.code
package snippet
// notice the package split into two lines, making the import shorter.
import myUser
class MySnippet {
render ={
"#message" #> "Your name: " + myUser.is
}
}

关于session - 我的 SessionVar 对象应该在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8305586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com