I have 2 variables in parameters map populated from a struts2 tag, as follows:
obj– A java object with various child objects. It can be treated as a hash in freemarker template.displayProperty– A string containing property name inside theobj. The name can be object graph containing multiple levels of drill down.
I want to display the given property of the obj using freemarker template. For example if displayProperty is entity.name, then it should essentially print value of obj.entity.name.
My freemarker template is ${obj[displayProperty]}, but it fails when displayProperty contains a dot.
One solution would be
('obj.' + displayProperty)?eval, although it involves generic expression parsing and so is not the fastest. The fastest running solution would be to create a custom method (TemplateMethodModelEximplementation) that you could use like${walkProperty(obj, displayProperty)}, which would split the string at the dots, and callTempalteHashModel.getin a loop.