scala 我可以在 Play Framework 的模板/视图中调用会话吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10888407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I call session in template/view on Play Framework
提问by wynnch
I'm new at using Play Framework 2.0 (I am using Scala) and have a question about sessions.
我是使用 Play Framework 2.0 的新手(我使用的是 Scala)并且有一个关于会话的问题。
I come from a Ruby on Rails background, so I tend to think of everything that I learn in Play Framework with respect to Ruby on Rails.
我来自 Ruby on Rails 背景,所以我倾向于将我在 Play Framework 中学到的所有内容都与 Ruby on Rails 相关联。
With that in mind, is there any way for me to call stuff stored in the Session while I am in the view?
考虑到这一点,有什么方法可以让我在视图中调用存储在 Session 中的内容?
If I have "hello" -> "world" stored in the Session, I want to be able to do something like @session.get("hello") and be able to use "world" in the view. Is this possible?
如果我在会话中存储了“hello”->“world”,我希望能够执行类似 @session.get("hello") 的操作,并且能够在视图中使用“world”。这可能吗?
The other option I see is to store the value in a variable in the controller, and pass that along to the view by doing something like OK( var ), but that way seems kind of clunky especially if I start using more variables.
我看到的另一个选项是将值存储在控制器的变量中,然后通过执行 OK(var) 之类的操作将其传递给视图,但这种方式似乎有点笨拙,尤其是当我开始使用更多变量时。
Thanks!
谢谢!
回答by James Ward
Sessions in Play store in cookies and are really just for cross-request data. If that is what you want then you can use @session.get("hello")but what you might actually be after is a way to pass stuff from controllers to templates without having to specify them as parameters. In that case, see the very detailed answer to that question here:
https://stackoverflow.com/a/9632085/77409
Play 中的会话存储在 cookie 中,实际上仅用于跨请求数据。如果这是您想要的,那么您可以使用,@session.get("hello")但您实际上可能追求的是一种将内容从控制器传递到模板的方法,而无需将它们指定为参数。在这种情况下,请在此处查看该问题的非常详细的答案:https:
//stackoverflow.com/a/9632085/77409
回答by biesior
Yes you can use @session.get("hello")in template, however it looks that you need to specify at least implicit parameter named 'session' at the beginning of the template when using templates with Scala controllers:
是的,您可以@session.get("hello")在模板中使用,但是在将模板与 Scala 控制器一起使用时,您似乎需要在模板的开头至少指定名为“session”的隐式参数:
@()(implicit session: play.api.mvc.Session)
Also there is flashscope - it differs from sessionthat it lives only for one request and is not signed. So it is most often used just for transporting error/inf messages.
还有flash范围 - 它不同于session它只存在于一个请求并且没有签名。所以它最常用于传输错误/inf 消息。
See Session and flashscopes doc
请参阅会话和闪存范围文档
Finally as each template is just a Scala function, you can also call some action from your controller and retrieve session data
最后,由于每个模板只是一个 Scala 函数,您还可以从控制器调用一些操作并检索会话数据
回答by venu
You can definetly call ursession in play templates.
您可以明确地ur在播放模板中调用会话。
Try this code - it works in views:
试试这个代码 - 它适用于视图:
$session.session_variable_name;

