0

i am passing a TreeMap to a JSP page. The map contains as keys the days of the current or the next month. The passed data has to be rendered in the JSP page. Therefore i need a way to access the .containsKey() or .containsValue() or the .get() method from the map. I tried these methods but i always get an exception.

When i'm using the .get(1) or .get("1") method i eclipse shows me a ClassCastException

java.lang.Integer cannot be cast to java.lang.Long

Any ideas how to solve my problem?

Edit: The Code:

public TreeMap<String, String> getCalendarEntries(
    boolean showNextMonth) throws Exception{
TreeMap<String, String> treeMap = new TreeMap<String, String>();

// doing some stuff... 

treeMap.put("2", "someValue"); 

// ..

return treeMap;
}

The controller

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String home(Locale locale, Model model) throws Exception {
// do stuff.. 

TreeMap<String, String> treeMap = getCalendarEntries(true);

model.addAttribute("treeMap", treeMap);
return "home"
}

The JSP Page

<c:forEach var="calendarEntry" items="${calendarEntries }">
${calendarEntry.getKey()}-${calendarEntry.getValue()}
// Returns the key/value mapping
</c:forEach>

${calendarEntries.get("2")}
// returns nothing. 
DwB
  • 37,124
  • 11
  • 56
  • 82
smsnheck
  • 1,563
  • 3
  • 21
  • 33

1 Answers1

0

The problem is your map structure is like this.

    Map<Long,Object> map =new HashMap<Long,Object>()
    map.put(1l,"value1");
    map.put(2l,"value2");

But you are accessing it using .get(1). here 1 is as an integer and system is trying to cast to long and it failed.

try .get(1l) instead of .get(1)

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64