Play Framework posted values revisited
For this quick sample, let’s define the following view:
app/views/index.scala.html
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | @(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
1 2 3 4 5 | # 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | 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:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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
1 | def values = form.bindFromRequest()(request).data |
You can also play with the tuples and issue something like
1 2 | val data = form.bindFromRequest.get Ok(views.html.index( "You are %s, %s" . format (data._2, data._1))) |
or
1 2 | 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:
1 | 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.