views:

108

answers:

1

Hi all,

I'm trying to access elements in a HashMap. The keys of this HashMap are defined by an Enum.

After going through the documentation, I figured that in order to be able to access the Enum, I have to send it to Freemarker like so:

BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
TemplateHashModel enumModels = wrapper.getEnumModels();
TemplateHashModel fieldTypeModel = (TemplateHashModel)enumModels.get("com.example.MinisiteFieldType");
root.put("fieldtypes", fieldTypeModel);

In my .ftl I tried the following:

${myelement.mymap[fieldtypes.SEOTEXT]}

However I get: Expression myelement.mymap[fieldtypes.SEOTEXT] is undefined on line X...

I wanted to make sure I mapped the enum correctly, so I tried:

${fieldtypes.SEOTEXT}

This didn't print anything, leaving me wondering wether it got through, or simply couldn't be printed.

In Java debug, just before inserting the TemplateHashModel in my root Map, 'fieldTypeModel' is filled with the correct data...

Any help would be appreciated!

Bart

A: 

I suspect your problem is that by using the [] syntax to access your map, you're implicitly telling Freemarker to treat it as a hash. That may not work as you'd expect -- the hash will probably represent the Map object, mapping 'size' and 'containsKey' and so forth to Java methods. Instead, try:

${myelement.mymap.get(fieldtypes.SEOTEXT)}


As a side note, I've never tried accessing enums by the method you describe, but there's another way to access Java constants from a template that doesn't require Java code, so the following should also work:

${myelement.mymap.get(stack.findValue("@com.example.MinisiteFieldType@SEOTEXT"))}
Toper