Reputation: 4371
what is the most efficient/elegant way to extract variables from a GET request?
Upvotes: 0
Views: 741
Reputation: 55798
There is better way than reading a queryString()
(it returns a Map
which you have to process manually). Use play.data.DynamicForm
instead:
public static Result aboutAMan() {
DynamicForm df = form().bindFromRequest();
int age;
String name;
boolean isAdmin;
name = (df.get("name") != null) ? df.get("name") : "The Unknown";
age = (df.get("age") != null) ? Integer.parseInt(df.get("age")) : 0;
isAdmin = Boolean.parseBoolean(df.get("is_admin"));
String about = (name + " is " + age + " years old and " + ((isAdmin) ? "is" : "isn't") + " an admin");
return ok(about);
}
of course you can also use shorter version while getting single params
public static Result aboutAMan() {
return ok("ellou' " + form().bindFromRequest().get("name"));
}
link is:
http://localhost:9000/about-a-man?name=SockSocket&age=23&is_admin=false
route is:
GET /about-a-man controllers.Application.aboutAMan
Upvotes: 2