Variable number of fields in form using struts 1.X
I need to build a form that loads a table that in each row contains a checkbox and an input text (the number of rows is variable, because it's loaded from a db). So my questions are:
- What fields shou开发者_StackOverflowld the associate formbean have ? ArrayLists ? One HashMap ?
- How can I know (once the form is submitted) what checkbox was selected so I consider the corresponding input text ?
I'm using struts 1.X as framework.
Thanks in advance!
Personally, I would use an array (list) for the checkboxes and a map for the input texts. You have to consider the fact that checkboxes are not sent on the request if they are not selected, but all your input texts are always sent. So, match the value of a checkbox with the map parameter of an input text, something like:
<input type="checkbox" name="ckName" value="val1" ../>
<input type="text" name="mapMethod(val1)" ../>
<input type="checkbox" name="ckName" value="val2" ../>
<input type="text" name="mapMethod(val2)" ../>
<input type="checkbox" name="ckName" value="val3" ../>
<input type="text" name="mapMethod(val3)" ../>
...
This means you will always have a map with all values:
val1 = "textbox 1 value"
val2 = "textbox 2 value"
val3 = "textbox 3 value"
...
and also have a list of selected checkboxes which can be:
[val1]
[val1, val2]
[val1, val2, val3]
... different combinations or []
You then keep the textbox values from the map only for the keys that are found in your list of checkbox values.
P.S. Remember also to reset your checkboxes.
精彩评论