Finally solved a very big problem for me... How do I render a page in spring framework that uses the thymeleaf templating engine? The string most probably will come from the database. After long hours of searching and trying, I've got it.
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.
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, MapSo to use it somewhere in your contoller, do something like this: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; } }
Mapcontext2 = new HashMap (); context2.put("object", object); service.getTemplateFromMap(datafromdb, context2);
Comments