Access Map<Enum, Object> in JSTL

enums, java, jsp, jstl

Solution

You can't. Your best bet is to change your map to use enum.name() as key:

HashMap<String, Long> map = new HashMap<String, Long>();
map.put(MyEnum.One.name, 1L);
map.addObject( "map", map);

Your first approach would work then:

${map['One']} // works now

Or you can write a custom EL function to do the above for you if you can't / don't want to change the map.

Problem

I have: ``` public enum MyEnum{ One, Two, Three } ``` From controller, I put in the model: ``` HashMap<MyEnum, Long> map = new HashMap<MyEnum, Long>(); map.put(MyEnum.One, 1L); mav.addObject( "map", map); ``` How do I in my JSTL access the object in the map for key enum MyEnum.One, in a neat way? ``` ${map['One']} //does not seem to work... ``` nor does ``` ${map[MyEnum.One]} ```

Original source