2

我有一个itemListand 对于每个item,都会显示一个评级下拉列表。在用户对每个人进行评分itemitemList,我想将这些费率存储在一个数组中。我该怎么做?selectedRate下面是Integer类型,代码未能解决问题。

<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>
4

1 回答 1

4

每个select选项都需要与特定项目相关联。

最简单的方法是使用Items 的集合,并为每个集合赋予Item一个rating属性。我Integer在这个例子中使用了一个。

使用<html:select>数组表示法,并直接设置每个项目的评级。(我使用的是表单本身的费率列表和更简单的布局;忽略这些差异。)

<logic:iterate id="item" name="ratesForm" property="itemList" indexId="i">
  ${item.name}&nbsp;
  <html:select property="itemList[${i}].rating">
    <html:optionsCollection name="ratesForm" property="rates" label="description" value="value" />
  </html:select>
  <br/>
</logic:iterate>

该操作按照我们的预期访问项目评级:

RatesForm ratesForm = (RatesForm) form;
List<Item> items = ratesForm.getItemList();
for (Item item : items) {
    System.out.println(item.rating);
}

如果项目没有关联的评级,则需要使用项目 ID 键和评级值的映射。这更令人费解。我推荐一个收藏。

首先,地图将是Map<String, Object>因为索引属性的工作方式。除了地图本身的普通 getter 之外,还提供索引方法:

private Map<String, Object> itemRatings;

public Map<String, Object> getItemRatings() {
    return itemRatings;
}

public Object getItemRating(String key) {
    return itemRatings.get(key);
}

public void setItemRating(String key, Object val) {
    itemRatings.put(key, val);
}

JSP 将类似,但使用"()" 而不是 "[]"使用索引表单方法。

<logic:iterate id="item" name="ratesForm" property="itemList">
    ${item.name}&nbsp;
      <html:select property="itemRating(${item.id})">
        <html:optionsCollection name="ratesForm" property="rates" label="description" value="value" />
      </html:select>
    <br/>
</logic:iterate>

提交表单时,itemRatings映射将包含代表每个项目 ID 的字符串键。键和值都是Strings,您需要手动转换为数值。

于 2011-11-26T17:45:42.780 回答