Wednesday, October 16, 2019

Rendering template from string using thymeleaf in spring

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.

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();
        return templateEngine;

   public String getTemplateFromMap(String htmlContent, Map dynamicAttributesMap){
        templateEngine = getTemplateEngine();
        String template = null;
        final Context ctx = new Context(new Locale(TEMPLATE_LOCAL));
        if(null != templateEngine){
              template = templateEngine.process(htmlContent,ctx);
        return template;
So to use it somewhere in your contoller, do something like this:
Map context2 = new HashMap();
context2.put("object", object);
service.getTemplateFromMap(datafromdb, context2);

Is Blogging No Longer a Thing?

As I embark on my new journey to learn the Rust programming language, I find myself pondering—where have all the blogs gone? In search of pr...