Play 2.0 Java: bind an array from a query

I'm ... stuck oO

I have parameters in an external request:

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

And I'm looking for any way to bind them, i.e. with DynamicForm .

I can get param with:

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

But, of course, I do not know the indexes, because they are selected in the client-side form created by an independent application.

When I try to use:

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

or even

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

Am I missing something really basic, or is Play simply unable to do this?

+6
source share
2 answers

Not the most beautiful way, but did you try to get the map keys returned by asFormUrlEncoded:

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

The reason this does not work is because the QueryStringBinder for List [String] or Array [String] does not exist in the structure. 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]

+5
source

Source: https://habr.com/ru/post/923185/


All Articles