1

Has anybody dealt with a situation, that on the client side (html form) there is one type of field, that exist various times?

Like:

    <input id="fileRef1" type="hidden"/>
    <input id="fileRef2" type="hidden"/>
    ....
    <input id="fileRefx" type="hidden"/>

so that the DTO would need something like String[] fileRefs;

I'm using spring framework, but I don't see how could I use dataBinder or custom editor for that. Any suggestions please?

oers
  • 18,436
  • 13
  • 66
  • 75
lisak
  • 21,611
  • 40
  • 152
  • 243

3 Answers3

2

Spring binding will allow you use syntax like

<input name="arrayOfStrings[0]" type="hidden" value="xxx">
<input name="arrayOfStrings[1]" type="hidden" value="xxx">

Just be sure your DTO array is the correct size. You can also use org.springframework.util.AutoPopulatingList if you do now know at form generation time how many elements your collection will have.

three-cups
  • 4,375
  • 3
  • 31
  • 41
  • @three_cups_of_java: what do you mean "correct size" ? dataBinder takes care about instantiation and initialization of "String[] fileRefs;" in DTO. As to the AutoPopulatingList, you mean that it is enough to define "AutoPopulatingList apList" in my DTO and then having various number of input fields like this arrayOfStrings[0-x] ? – lisak Feb 25 '11 at 01:09
  • By "correct size", I mean that if you have an array or java.util.List of size 0, you will not be able to add anything to it because it has no "slots" to accept values. – three-cups Feb 25 '11 at 02:37
  • I'm not sure about the data binder instantiating a String[]. I've never done this, but it makes sense to me (javax.servlet.ServletRequest.getParameterValues returns a String[]) – three-cups Feb 25 '11 at 02:40
  • AutoPopulatingList is good for rich DTOs (Java Bean with getters and setters). It generally requires an ElementFactory (see the API) – three-cups Feb 25 '11 at 02:42
  • I see, you meant that I'll get it straight from the servlerRequest and populate the array by myself. I thought you were talking about dataBinding. That I define a DTO and dataBinder populates the entire DTO instance from the request for me. – lisak Feb 25 '11 at 09:49
  • you're not explaining yourself much, first off, you are talking about "spring binding", then about getting the parameters right from servletRequest by yourself. And the correct answer should be rather to name all input fields the same name="a", name="a", name="a" with different ids. – lisak Feb 25 '11 at 11:48
0

Give the same field name to all fields

<input id="fileRef1" name="fileRefs" type="hidden"/>
<input id="fileRef2" name="fileRefs" type="hidden"/>

....

so that fileRefs value will save in String array like String[] fileRefs in order

Nagaraju Badaeni
  • 890
  • 8
  • 14
-1

You should probably take a look at these links :

SO

mattfleming

mattfleming - why ?

eggsylife

lifeinide

richardadamdean

Community
  • 1
  • 1
lisak
  • 21,611
  • 40
  • 152
  • 243