0

从 GET 请求中提取变量的最有效/优雅的方法是什么?

4

2 回答 2

2

有比读取 a 更好的方法queryString()(它返回Map您必须手动处理的 a)。改用play.data.DynamicForm

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);
}

当然,您也可以在获取单个参数时使用较短的版本

public static Result aboutAMan() {
    return ok("ellou' " + form().bindFromRequest().get("name"));
}

链接是:

http://localhost:9000/about-a-man?name=SockSocket&age=23&is_admin=false

路线是:

GET    /about-a-man     controllers.Application.aboutAMan
于 2012-09-10T17:53:16.153 回答
1

成立。只需使用 request().queryString()

于 2012-09-10T16:14:10.560 回答