我将TreeMap传递给JSP页面。地图包含当前或下个月的日期作为键。传递的数据必须在JSP页面中呈现。因此,我需要一种方法来从地图访问.containsKey()
或.containsValue()
或.get()
方法。我尝试了这些方法,但我总是遇到异常。
当我使用.get(1)
或.get("1")
方法时,eclipse会向我显示ClassCastException
java.lang.Integer cannot be cast to java.lang.Long
任何想法如何解决我的问题?
编辑:代码:
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;
}
控制器
@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"
}
JSP页面
<c:forEach var="calendarEntry" items="${calendarEntries }">
${calendarEntry.getKey()}-${calendarEntry.getValue()}
// Returns the key/value mapping
</c:forEach>
${calendarEntries.get("2")}
// returns nothing.
答案 0 :(得分:0)
问题是你的地图结构是这样的。
Map<Long,Object> map =new HashMap<Long,Object>()
map.put(1l,"value1");
map.put(2l,"value2");
但您使用.get(1)
访问它。这里1
是一个整数,系统正在尝试强制转换为失败。
尝试.get(1l)
而不是.get(1)