Wednesday, August 13, 2008

EL Map won't work with numerical index

Assume you're using JSTL in a JSP. You create a Map in the JSP's backing class (e.g. SimpleFormController in Spring):

Map map = new HashMap();
map.put(0,115);
map.put(1,226);

This map is provided to your JSP as a variable with the name exampleMap. You then attempt to access the map's values in the JSP:

${exampleMap[0]}
${exampleMap[1]}

It doesn't work. For an explanation, see this dialogue which does a phenomenal job of describing the reason.