Play Framework posted values revisited
For this quick sample, let’s define the following view:
app/views/index.scala.html
@(message: String) message: @message <br /> <h2>Scala form</h2> <form action="@routes.ScalaPoster.save()" method="POST"> scala name: <input name="scala_name"> <br /> scala surname: <input name="scala_surname"> <br /> <input type="submit" value="save"> </form> <h2>Java form</h2> <form action="@routes.JavaPoster.save()" method="POST"> java name: <input name="java_name"> <br /> java surname: <input name="java_surname"> <br /> <input type="submit" value="save"> </form>
And the following routes file:
conf/routes
# Home page GET / controllers.Application.index POST /scala controllers.ScalaPoster.save POST /java controllers.JavaPoster.save
With java, accesing directly the request body:
app/controllers/JavaPoster.java
package controllers; import play.mvc.*; import views.html.*; import java.util.Map; public class JavaPoster extends Controller { public static Result save() { final Map<String, String[]> values = request().body().asFormUrlEncoded(); final String name = values.get("java_name")[0]; final String surname = values.get("java_surname")[0]; return ok(index.render(String.format("You are %s, %s",surname, name))); } }
Or using a DynamicForm:
package controllers; import play.mvc.*; import views.html.*; import play.data.DynamicForm; public class JavaPoster extends Controller { public static Result save() { final DynamicForm form = form().bindFromRequest(); final String name = form.get("java_name"); final String surname = form.get("java_surname"); return ok(index.render(String.format("You are %s, %s",surname, name))); } }
Now the scala version, accessing the body:
app/controllers/ScalaPoster.java
package controllers import play.api.mvc._ object ScalaPoster extends Controller { def save = Action { request => def name = request.body.asFormUrlEncoded.get("scala_name")(0) def surname = request.body.asFormUrlEncoded.get("scala_surname")(0) Ok(views.html.index("You are %s, %s".format(surname, name))) } }
And defining a form
package controllers import play.api.mvc._ import play.api.data.Form import play.api.data.Forms.tuple import play.api.data.Forms.text object ScalaPoster extends Controller { val form = Form( tuple( "scala_name" -> text, "scala_surname" -> text ) ) def save = Action { implicit request => def values = form.bindFromRequest.data def name = values("scala_name") def surname = values("scala_surname") Ok(views.html.index("You are %s, %s".format(surname, name))) } }
Notice the implict request in the above sample. You could explicitly pass it to bindFromRequest with
def values = form.bindFromRequest()(request).data
You can also play with the tuples and issue something like
val data = form.bindFromRequest.get Ok(views.html.index("You are %s, %s".format(data._2, data._1)))
or
val (name, surname) = form.bindFromRequest.get Ok(views.html.index("You are %s, %s".format(surname, name)))
And of course, if you just want to read a single posted value you could issue:
def name = Form("scala_name" -> text).bindFromRequest.get
There are several ways to achieve it. I hope this serves as a useful reminder.
Reference: Play Framework posted values revisited from our JCG partner Sebastian Scarano at the Having fun with Play framework! blog.