1.
您的 HTML 中有一些错误:
<li><a href="${url_itemmarket}/${cmenu.itemName}">${cmenu.description}/a>/li>
^ ^
| |
two errors here (mising < characters) --------------------------------
replace with this -----------------------------------------------------
| |
v v
<li><a href="${url_itemmarket}/${cmenu.itemName}">${cmenu.description}</a></li>
2.
您应该使用Map。
地图的键应该是不同的类型。
这些值应该是Food 对象列表。
然后您可以在您的 JSP 中遍历映射的键。
您需要一个嵌套循环来遍历每个列表中的食物。
我认为您的 JSP/JSTL 看起来像这样,但未经测试:
<table>
<tr><th>type</th><th>Item Name</th></tr>
<!-- iterate over each key in the map -->
<c:forEach var="foodMapEntry" items="${foodMap}">
<tr>
<td>${foodMapEntry.key}</td>
<td>
<!-- iterate over each item in the list of foods -->
<c:forEach var="food" items="${foodMapEntry.value}">
| ${food.name}<br/>
</c:forEach>
</td>
</tr>
</c:forEach>
</table>
下面是一些代码,展示了如何构建上面使用的地图:
/* create a list of food */
List<Food> foodList = new ArrayList<Food>();
/* add some fruits to the list */
foodList.add(new Food("Banana", "fruit"));
foodList.add(new Food("Apple", "fruit"));
/* add some veggies to the list */
foodList.add(new Food("Onion", "vegetable"));
foodList.add(new Food("Mushroom", "vegetable"));
/* add some candy to the list */
foodList.add(new Food("Chocolate", "candy"));
foodList.add(new Food("Gummy Bears", "candy"));
/* create a Map that maps food types to lists of Food objects */
Map<String, List<Food>> foodMap = new HashMap<String, List<Food>>();
/* populate the map */
for (Food f : foodList) {
String foodType = f.getType();
if (foodMap.containsKey(foodType)) {
foodMap.get(foodType).add(f);
}
else {
List<Food> tempList = new ArrayList<Food>();
tempList.add(f);
foodMap.put(foodType, tempList);
}
}
还有一个简单的 Food 类:
class Food {
private String name;
private String type;
public Food(String n, String t) {
name = n;
type = t;
}
public String getName() { return name; }
public String getType() { return type; }
}
这是关于将地图与 JSP 和 JSTL 一起使用的问题/答案。