First I've got a clue from this particular stackoverflow question. But following an example from there got me an error about class not found for ognl.PropertyAccessor. That solution was found here.
So here's how I finally done it...
First thing is to create a service where that service can be used wherever you need it in your spring app.
@Service
public class PortalService {
private TemplateEngine templateEngine;
private final static String TEMPLATE_LOCAL = "US";
private TemplateEngine getTemplateEngine(){
if(null == templateEngine){
templateEngine = new TemplateEngine();
StringTemplateResolver templateResolver = new StringTemplateResolver();
templateResolver.setTemplateMode(TemplateMode.HTML);
templateEngine.setTemplateResolver(templateResolver);
}
return templateEngine;
}
public String getTemplateFromMap(String htmlContent, Map dynamicAttributesMap){
templateEngine = getTemplateEngine();
String template = null;
final Context ctx = new Context(new Locale(TEMPLATE_LOCAL));
if(!CollectionUtils.isEmpty(dynamicAttributesMap)){
dynamicAttributesMap.forEach((k,v)->ctx.setVariable(k,v));
}
if(null != templateEngine){
template = templateEngine.process(htmlContent,ctx);
}
return template;
}
}
So to use it somewhere in your contoller, do something like this:
Mapcontext2 = new HashMap (); context2.put("object", object); service.getTemplateFromMap(datafromdb, context2);