我可以在Play框架的模板/视图中调用会话吗 [英] Can I call session in template/view on Play Framework

查看:68
本文介绍了我可以在Play框架的模板/视图中调用会话吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Play Framework 2.0(我正在使用Scala),并且对会话有疑问.

I'm new at using Play Framework 2.0 (I am using Scala) and have a question about sessions.

我来自Ruby on Rails背景,因此我倾向于考虑在Play Framework中从Ruby on Rails中学到的所有知识.

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.

考虑到这一点,当我在视图中时,有什么方法可以调用存储在Session中的东西吗?

With that in mind, is there any way for me to call stuff stored in the Session while I am in the view?

如果我在Session中存储了"hello"->"world",我希望能够执行类似@ session.get("hello")的操作,并能够在视图中使用"world".这可能吗?

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?

我看到的另一种选择是将值存储在控制器中的变量中,并通过执行OK(var)之类的操作将其传递给视图,但是这种方式似乎有些笨拙,尤其是当我开始使用更多变量时

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.

谢谢!

推荐答案

Play中的会话存储在cookie中,并且实际上仅用于交叉请求数据.如果这是您想要的,则可以使用@session.get("hello"),但是实际上可能需要的是一种将内容从控制器传递到模板的方法,而不必将它们指定为参数.在这种情况下,请在此处查看该问题的非常详细的答案: https://stackoverflow.com/a/9632085/77409

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框架的模板/视图中调用会话吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆