Play でのセッションは Cookie として実現されている。
Play でセッションを使う場合にはこのようなコードになる。
package controllers import play.api._ import play.api.mvc._ import play.api.mvc.RequestHeader object Application extends Controller { def index = Action.apply { request => val currentVal : String = request.session.get("hoge") match { case Some(x) => x; case None => ""; } Ok("hoge = " + currentVal).withSession( request.session + ("hoge" -> "hogege")); } }
request.session.get
は Option を返すので、
パターンマッチで Some
か None
かを判定して、Some
から値を取り出す必要がある。もしくは、Option.getOrElse を使って、
... val currentVal : String = request.session.get("hoge").getOrElse(""); ...とする。こっちの方が簡単でいい。