I want to be able to post a form that has indexed array fields, and read those fields from Sparkjava. Based on some reading around, I've found a handful of recommendations to use square brackets to index my repeated fields. This looks like a php convention that automatically parses the repeated field into an array. However I can't figure out what magic Sparkjava is doing to this field, it seems to ignore the brackets and presents me with a null object for that key.
Since that was clear as mud, here is some code:
This is my form:
<form method="post">
Array: <input type="text" name="MyArray[1]" /><br>
Array: <input type="text" name="MyArray[2]" /><br>
Name: <input type="text" name="name" /><br>
<button type="submit">Submit</button>
</form>
And this is my form handling endpoint:
post("/form", (req, res) -> {
String data = req.queryMap().toMap().toString();
return data;
});
When I enter values into my form and submit, the resulting page displays:
{name=[Ljava.lang.String;@7ae881bf, MyArray=null}
So it parses name
as a key (just like any boring form) but looks like it parses the trailing []
on the MyArray field as a signifier of some special structure that should be parsed into a specific object. It behaves the same if I leave out the indexes, and name both fields MyArray[]
. I would be happy if it let me access the keys as MyArray[1]
and MyArray[2]
, but I would prefer to understand what sort of parsing magic Sparkjava is trying to do so I can leverage it.
Thanks!