Question

In playframework 2, the session and cookie is modified available in next session but not current session.

like code:

Ok(views.html.something("Hello from Scala")).withSession(("user"->"Admin"))

this session will be modified and presented in next session. Can I made some Cookie which can available in current session?

I do not want to use parameter to modify it for the reason is if I use parameter as user login and role indentified Tag, it would be added all page control parameters table. I want to do it as an AOP, injected in session and then only look at session to find out the request come from where and how to response it in template frame not in content. Any one have better solution for this issue are also welcome to post.

Was it helpful?

Solution

Play's session is a client-side session stored in a cookie, and sent to the client with every response. Since you can only modify the session when you actually answer, any changes you want to make use of in the current action (request - response cycle) should probably be available to you in that specific action anyway.

If your aim is to look at the session inside of a template, then I would suggest you explicitly pass a copied version of the session to the template and also set it as a new session.

Controller action:

def someAction = Action { implicit request =>
  val updatedSession = request.session.copy(Map("user" -> "Admin")
  Ok(views.html.something("Hello from Scala")(updatedSession)).withSession(updatedSession)
}

Template:

@(text: String)(implicit session: Session)

<h1>@text</h1>
@session.get("user").map { u=>
  Your role is: @u
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top