我有一个类型为allList
的Hashmap对象HashMap<String,ArrayList<Item>>
。我想把它作为jquery accordion显示在我的JSP页面上。以下是我尝试过的代码。
<script type="text/javascript">
$(function() {
$( "#accordion" ).accordion({
heightStyle: "fill",
collapsible: true
});
});
</script>
<div id="accordion">
<c:forEach items="${allList}" var="myLs">
<h3>${myLs.key}</h3>
<div>${myLs.value}</div> // This is giving me toString of Item.
</c:forEach>
</div>
我能够将hashmap的键显示为标题。但我无法弄清楚如何将相应的arraylist对象显示为有序列表。请帮帮我。
public class Item implements java.io.Serializable, Comparable<Object> {
private Long id;
private String itemName;
private Double unitCost;
private String status;
private int quantity;
public Item() {
}
//getters and setters
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Item)) {
return false;
}
final Item item = (Item) o;
if (getItemName() != null && item.getItemName() == null)
return false;
if (getItemName() == null && item.getItemName() != null)
return false;
if (!getItemName().equals(item.getItemName()))
return false;
return true;
}
public int hashCode() {
return getItemName().hashCode();
}
public String toString() {
return "Item - Id: "+getId+", Name : "+getItemName;
}
public int compareTo(Object o) {
if (o instanceof Item) {
return getItemName().compareTo(((Item) o).getItemName());
}
return 0;
}
}
您将使用第二个forEach循环:
<div id="accordion">
<c:forEach items="${allList}" var="myLs">
<h3>${myLs.key}</h3>
<div>
<c:forEach var="item" items="${myLs.value}">
${item.foo}, ${item.bar} <br/>
</c:forEach>
</div>
</c:forEach>
</div>
我认为你的错误命名选择使你感到困惑。你应该'命名一个Map<String, ArrayList<Item>>
allList
,因为它不是列表,而是地图。你不应该将地图条目命名为myLs
,因为它没有任何意义。我会重构代码(例如,假设地图中的键代表项目的所有者)
<div id="accordion">
<c:forEach items="${itemsPerOwner}" var="itemsPerOwnerEntry">
<h3>${itemsPerOwnerEntry.key}</h3>
<div>
<c:forEach var="item" items="${itemsPerOwnerEntry.value}">
${item.foo}, ${item.bar} <br/>
</c:forEach>
</div>
</c:forEach>
</div>