3

I tried to access session attribute using scala and play frame work. but could n`t get success. here is the code

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"-> "[email protected]")

                }else{
                   Redirect(routes.Users.loginUser()).
                    flashing("error" ->"Invalid Login")
                }    
            }
            )
    }

view ---- > 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>

}

1 Answer 1

4

If you need the session on the view, you will need to add the implicit request (implicit request: play.api.mvc.RequestHeader). So your code will be something like

    @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>

    }
Sign up to request clarification or add additional context in comments.

6 Comments

I think you can add the implicit directly to the Session object instead: @()(implicit session: Session).
I don't think it can work using the session as implicit with the current code. It would need to be declared. However, we already have the request object in the first line: def login = Action { implicit request =>
I was talking about the view code and not the controller. As documented here, you can implicitly add Flash scope as a parameter. I suppose you can do the same with Session scope.
Well, the Flash parameter is kind of a special object. You need to use Result.flashing to make it implicitly available. Perhaps I didn't explain my solution well enough. The view uses what you declare in the Action as a parameter (e.g.: @(testObject: MyObject)) or what you already have declared implicitly as the second parameter at the head declaration (e.g: @()(implicit testObject: MyObject)). If @marcospereira still have doubts about this, perhaps I can extend this with another comment.
Carlos, here is what I'm talking about: gist.github.com/anonymous/f30fba1c385303f0b34d. In other words, just like you can implicitly pass a Flash object to the view, you can do the same with a Session. Notice that flashing is just a idiomatic way to set values in Flash scope, just like withSession.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.