10
votes

I have a Webapp that is built on top of the Play framework and Scala. It is about presenting the user with a set of questions with each question having a set of answers. Some of the questions have radio button types an answers and some have check boxes as answers. When the user clicks start test, I call the controller, fetch the list of questions with its answers and return the result as a case class to the view template. I now need to maintain the state of the test as the user answers each question. He can go previous, next and I need to keep track of all the questions that he answered.

Coming from a Java EE background, I thought I can store the case class in the session and manipulate it in my controller. But that unfortunately does not look like that as the Play framework's session is a key value pair of String, String and not a String, Object. I'm now stuck with my app and since my experience with the Play framework is limited, I would like to ask for suggestions.

3

3 Answers

11
votes

There is no state at all in Play framework so if you want to keep some data across multiple HTTP requests its handy to use Session scope that actually creates cookies with key/value pair (String, String) and they are limited to 4KB size.

My suggestion is to do it with Json, Play-json library is awesome. IF you have models with JSON Read/Write/Format combinators than its simple.

Ok(render(Questions)).withSession("answers" -> Json.prettyPrint(Json.toJson(Answer)))

Reading a session value can be done like this:

def index = Action { implicit request =>
      session.get("answers").map { answers =>
        val jsValueAnswers: JsValue = Json.parse(answers)
        val answersModel: YourAnswerModel = Json.fromJson(jsValueAnswers) 
        Ok("Got previous answers and created session cookie with them")
        .withSession("answers2" -> Json.prettyPrint(Json.toJson(answersModel)))
      }
    }

Hope this help you a bit.

Cheers

8
votes

Play only supports Strings for sessions because it stores all session state in cookies - this means Play nodes can scale without the need for any sort of clustering/state sharing tech.

If the data you want to store is small (less than 2k), then just serialise it into JSON, or it sounds like in your case, even simpler would be to serialise it into a comma separated list of answers.

Otherwise, you can store it in a cache such as memcached or redis.

4
votes

Play is a framework designed to be stateless, so you do not find any concrete concept of server side session. Of course, this is not strictly prohibited, if you need it, as in this case, you can use a server-side technology (cache or database) as a container for your items and Play-cookie session for storing the access key string.

This is an example of storing object using Play Cache:

import play.api.cache.Cache
import play.api.Play.current

val key = UUID.randomUUID.toString
Cache.set(key, yourModel, 0)
Ok.withSession("answer_key" -> key)

and this is an example of retrieval:

import play.api.cache.Cache
import play.api.Play.current

val key = session.get("answer_key").getOrElse("none")
val yourModel = Cache.getAs[ModelClass](key).getOrElse(//whatever you want if not exists)