views:

545

answers:

1

Does JSP or any related lightweight technology like JSTL perform HTTP POST "data grouping", or support form element "indexing" in the way PHP does?

For example, you can create an HTML form with the following inputs:

<input type="text" name="person[1][name]" />
<input type="text" name="person[1][age]" />
<input type="text" name="person[2][name]" />
<input type="text" name="person[2][age]" />

... and PHP will parse that into a nested associative array automatically. Do JSP, Java Servlets, or any related spec or tool provide this kind of translation out of the box?

The goal is to submit multiple "record groups" in a single form, and process them server-side in JSP or a Servlet.


Requirements:

  • The functionality cannot rely on JavaScript
  • No full frameworks like Spring, Struts, or the like
  • I'm trying to avoid reinventing the wheel with my own naming convention and manual String parsing / Regex


Related Links:

+4  A: 

Try this,

<input type="text" name="personNames" />
<input type="text" name="personAges" />
<input type="text" name="personNames" />
<input type="text" name="personAges" />

You should consider to create input fields using a loop, you don't need to postfix the name even. and get parameter values like this in your servlet,

String[] names = request.getParameterValues("personNames");
String[] ages = request.getParameterValues("personAges");

It will come in the same order as defined in your HTML. Then loop over it like below,

for( String name : names) {
   System.out.println(name);
}
Adeel Ansari
Good thought, but I did try that earlier. When one of the inputs is a checkbox, the sets get out of sync. non-checked boxes don't get submitted, throwing off the count. e.g., if you have 10 records with checkboxes and select the last 5, they come through as having selected the first 5
drfloob
In that case, the value of the checkbox will tell you which one get submitted. Anyway, those you are not getting means, they don't have any values. So, why bother. You can assume all those null, why not?
Adeel Ansari
@Vinegar: it means that checkboxes have to be handled differently. With checkboxes, the suggestion you have above won't work, as you'll get an array of yes checkboxes. you'd have to do <input type="checkbox" name="personAgreed" value ="[insert number using code]">, and then read out the numbers instead.
Stobor
@Stobor: Of course, otherwise they don't show any distinction. Whereas in the case of input type text, the parameter will be posted in request, no matter if its empty/null. So, you can easily know which one was left empty.
Adeel Ansari
@Vinegar: it's a fine kludge, maintaining parallel arrays and synchronizing a checkbox value in code to array indexes. It would work, anyway, not unlike the counter-prepended name hack I am hoping to avoid. The question is: is this the best way JSP has? Is there a better, builtin, or standard way in JSP / JSTL / etc. that I just haven't found yet? I thought for sure that there would already be a better solution in the wild for this not-so-uncommon problem, despite my inability to uproot one this whole week =)
drfloob
Few facts, 1. There is no need to come up with your own naming convention, so you should be happy. 2. There is no string manipulation using regex or something, so you should be happy again, 3. It doesn't rely on JavaScript, so you should be happy once more. 4. The solution is not introducing any full blown framework, <repeat-it-yourself>. 5. Its similar in the way PHP does, as per your desire. Now where is the problem?
Adeel Ansari
There is no other better way, until you choose a framework route.
Adeel Ansari