我尝试使用scala访问会话属性并播放框架工作。但是不可能获得成功。这是代码
User.scala
def login = Action { implicit request =>
val newLoginForm = loginForm.bindFromRequest()
newLoginForm.fold(
hasErrors = { form =>
Redirect(routes.Users.loginUser()).
flashing(Flash(form.data) +
("error" -> "Fill user name and Password"))
},
success = { implicit newUser =>
if(User.findUserBydb(newUser.uname,newUser.pass)){
Ok(views.html.pages.page_one()).withSession("mysession"-> "jeff@example.com")
}else{
Redirect(routes.Users.loginUser()).
flashing("error" ->"Invalid Login")
}
}
)
}
查看----> page_one.scala.html
@import play.api.Play.current
@import play.api.i18n.Messages.Implicits._
@import play.api.mvc.Session
@import play.api.mvc.Request
@index("Hi"){
<h2>
Welcome @request.session.get("mysession")
</h2>
}
答案 0 :(得分:4)
如果您需要视图上的会话,则需要添加隐式请求(implicit request: play.api.mvc.RequestHeader)
。所以你的代码就像
@import play.api.Play.current
@import play.api.i18n.Messages.Implicits._
@import play.api.mvc.Session
@import play.api.mvc.Request
@()(implicit request: play.api.mvc.RequestHeader)
@index("Hi"){
<h2>
Welcome @request.session.get("mysession")
</h2>
}