Play 2.0 Java: Bind an array from request

2019-02-13 14:50发布

问题:

I... stuck o.O

I've params in foreign request:

param[62537]=abc;
param[20356]=cde;
param[92837]=fgh;

And I'm looking for any way for binding them ie. with DynamicForm.

I can get param with:

DynamicForm dynamicForm = form().bindFromRequest();
String firstParam = dynamicForm.field("param[62537]").value();

But of course I dont know the indexes as they are selected within the client-side form created by the independent app.

When I'm trying to use:

String[] firstParam = dynamicForm.field("param").value(); // it's NULL
String[] firstParam = dynamicForm.get("param"); // it's NULL

or even

String[] params = request().body().asFormUrlEncoded().get("param");
     // it's still NULL

Did I miss something really basic, or Play just can't do that?

回答1:

Not the prettiest way, but did you try to get the keys of the Map returned by asFormUrlEncoded:

Set<String> keys = request().body().asFormUrlEncoded().keySet();
for (String key : keys) {
     // check if key begin with "param["
}


回答2:

The reason why this does not work is that there is no QueryStringBinder for List[String] or Array[String] available yet in the framework. As far as I know, this is planned for Play 2.1.

If you need it now, you can try the solution shown here: QueryStringBinder for List[String]