I have a form with a few predefined textboxes and now apart from that I have also created some dynamic textboxes and I can do it with javascript (I guess). How to set the value of a dynamically generated textbox to a bean when a form is submitted. In the bean, I defined a string array to hold the contents of the dynamically generated textbox value. I am not using any framework, please guide me how to do this?
P粉8845486192023-12-12 00:47:45
You can give all input fields the same name and then use request.getParameterValues() to get all values in the order they appear in the HTML DOM tree.
For example (JavaScript generation)
<input type="text" name="foo" />
<input type="text" name="foo" />
<input type="text" name="foo" />
...
and
String[] values = request.getParameterValues("foo"); // ...
You can also add incrementing numbers after the name, such as foo1
, foo2
, foo3
, etc. and collect the values in a loop until ## is received #null.
<input type="text" name="foo1" />
<input type="text" name="foo2" />
<input type="text" name="foo3" />
...
and
List<String> foos = new ArrayList<String>(); for (int i = 1; i < Integer.MAX_VALUE; i++) { String foo = request.getParameter("foo" + i); if (foo == null) break; foos.add(foo); } // ...