简体   繁体   English

在JSP中处理复杂的HashMap显示

[英]Handling complex HashMap display in JSP

I have a Hashmap object allList in the form with type HashMap<String,ArrayList<Item>> . 我有一个Hashmap对象allList ,形式为HashMap<String,ArrayList<Item>> I want to display it my JSP page as jquery accordion . 我想将其显示在我的JSP页面上,为jquery手风琴 Below is the code I have tried. 以下是我尝试过的代码。

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

I am able to display hashmap's keys as header. 我能够将hashmap的键显示为标题。 But I am unable to figure out how to display the corresponding arraylist object as ordered list. 但是我无法弄清楚如何将相应的arraylist对象显示为有序列表。 Please help me out. 请帮帮我。

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;
    }
}

You would use a second forEach loop: 您将使用第二个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>

I think you're confusing yourself by your bad naming choices. 我认为您会因错误的命名选择而感到困惑。 You should' name a Map<String, ArrayList<Item>> allList , since it's not a list, but a map. 您应该为Map<String, ArrayList<Item>> allList ,因为它不是列表,而是地图。 And you shouldn't name a map entry myLs since it doesn't mean anything. 而且,您不应将映射条目命名为myLs因为它并不代表任何含义。 I would refactor the code to (assuming the key in the map, for example, represents the owner of the items) 我将代码重构为(假设地图中的键代表项目的所有者)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM