Access hashmap value by variable in JSP
Asked Answered
G

3

5

I have a hashmap, which is put to the request:

HashMap<Integer, String> myMap = ...
request.setAttribute("myMap", myMap);

In JSP I have a foreach loop

<c:forEach items="${list}" var="item" varStatus="status">
   <c:out value="${item.description}"/>
   <c:out value="${myMap[item.id]}"/>
</c:forEach>

but ${myMap[item.id]} does not work. How can I access hashmap's value by item.id variable ?

Gabrielson answered 25/1, 2011 at 10:37 Comment(0)
O
3

In EL, numbers are treated as Long. Change your Map to be a Map<Long, String> and it'll work.

Orelle answered 25/1, 2011 at 11:42 Comment(1)
Is there a special case for HashMap<String, some bean> because i tried this ${applicationscope.coursesHT[anotherbean.courseC]} to get the description of course but it didnot work but when i use c:set and c:out it worked !! :) thanksBotch
K
3

I think the id attribute of beans is not a wrapper object (Integer id;). Have a look at doc page of Map.

Text from JavaDoc

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on a such a map.

Item.java

package com.me;

public class Item {
    private Integer id;
    private String description;

    public Item() {
    }

    public Item(Integer id, String description) {
        this.id = id;
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

}

JSP snippet

<%
HashMap<Integer, String> myMap = new HashMap<Integer, String>();
myMap.put(new Integer(1), "One");
myMap.put(new Integer(2), "Two");
myMap.put(new Integer(3), "Three");
request.setAttribute("myMap", myMap);

List<com.me.Item> list=new ArrayList<com.me.Item>();
list.add(new com.me.Item(1,"A - Desc"));
list.add(new com.me.Item(2,"B - Desc"));
list.add(new com.me.Item(3,"C - Desc"));
request.setAttribute("list", list);
%>

<c:forEach items="${list}" var="item" varStatus="status">
  <c:out value="${item.description}"/>
  <c:out value="${myMap[item.id]}"/>
</c:forEach>
Kilgore answered 25/1, 2011 at 12:1 Comment(0)
M
3

You can put the key-value in a map on Java side and access the same using JSTL on JSP page as below:

Prior java 1.7:

Map<String, String> map = new HashMap<String, String>();
map.put("key","value");

Java 1.7 and above:

Map<String, String> map = new HashMap<>();
map.put("key","value");

JSP Snippet:

<c:out value="${map['key']}"/>
Manoeuvre answered 23/7, 2014 at 5:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.