Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.0 Java: Bind an array from request

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?

like image 809
biesior Avatar asked Aug 18 '12 13:08

biesior


2 Answers

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["
}
like image 145
ndeverge Avatar answered Nov 08 '22 02:11

ndeverge


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]

like image 5
hiltym Avatar answered Nov 08 '22 04:11

hiltym