dmnlk
dmnlk

Reputation: 3055

How to access enum Map in velocity html template?

I want to access java.util.EnumMap in velocity Html template.

enum Test {
    HUMAN,
    ANIMAL;
}



EnumMap<Test, Integer> map = new EnumMap<>(Test.class);
map.put(Test.HUMAN, 100);
map.put(Test.ANIMAl, 0);
responseData.setMap(map);

in html....

$!{responseData.(HUMAN)}; //dont work.

Is Velocity template only can get Map's value through String or Number key ?

Upvotes: 0

Views: 3676

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149145

Here is what I am sure concerning Velocity and java enums (say vc is the VelocityContext) : you can put an enum value in the context and use it normally in a velocity template (including as a key for an EnumMap) :

vc.put("human", Test.HUMAN);
map.put(Test.HUMAN, 100)
vc.put("map", map)
  • $map[$human] gives 100
  • $human.name() gives HUMAN
  • $human.ordinal() gives 0

But I found no solution to use directly $Test.HUMAN in the template (vc.put("Test", Test.class) is accepted but in template $!{Test.HUMAN} is empty).

Upvotes: 1

Related Questions