I have an itemList and for each item, a dropdown list of ratings is displayed. After user rates each item in itemList, i want to store those rates in an array. How can I do it? selectedRate below is of Integer type, and the code failed to solve the problem.
<logic:iterate id="item" name="itemList">
<tr>
<td>
<html:select name="aForm" property="selectedRate">
<html:optionsCollection name="allRates" label="description" value="value" />
</html:select>
</td>
</tr>
</logic:iterate>
Each
selectoption needs to be associated with a specific item.The easiest way is to use a collection of
Items, and give eachItemaratingproperty. I used anIntegerfor this example.The
<html:select>uses array notation, and directly sets each item's rating. (I'm using a list of rates from the form itself, and a simpler layout; ignore those differences.)The action accesses the item ratings as we'd expect:
If the items do not have an associated rating, you'll need to use a map of item id keys and rating values. This is more convoluted; I recommend a collection.
First, the map will be
Map<String, Object>because of the way indexed properties work. In addition to a normal getter for the map itself, provide indexed methods:The JSP will be similar, but use
"()"instead of"[]"to use the indexed form methods.When the form is submitted, the
itemRatingsmap will contain string keys representing each item's ID. Both the key and the value will beStrings, and you'll need to make any conversions to numeric values manually.