Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a Thymeleaf template from DB in Thymeleaf 3.0.5?

We're upgrading from Thymeleaf 2.1 to 3.0.5. Our current set up (before upgrading) has many thymeleaf templates defined and stored in a database table.
When we attempt to upgrade to 3.x our 2.1 code no longer works...ok fine but we can't find any good examples on how to do basically the same thing with Thymeleaf 3.0.5. Has anyone implemented this?

Even a decent example of how to implement org.thymeleaf.templateresolver.StringTemplateResolver would probably push us in the right direction...but we can't find anything on that either.

This is what we used in 2.1:

public class ThymeleafTemplateResolver extends TemplateResolver {

    private final static String PREFIX = ""; 

    public ThymeleafTemplateResolver() {
        setResourceResolver(new DbResourceResolver());
        setResolvablePatterns(Sets.newHashSet(PREFIX + "*"));
    }

    @Override
    protected String computeResourceName(TemplateProcessingParameters params) {
        String templateName = params.getTemplateName();
        return templateName.substring(PREFIX.length());
    }

    private class DbResourceResolver implements IResourceResolver {

        @Override
        public InputStream getResourceAsStream(TemplateProcessingParameters params, String template) {
            ThymeleafTemplateDao thymeleaftemplateDao = ApplicationContextProvider.getApplicationContext().getBean(ThymeleafTemplateDao.class);
            ThymeleafTemplate thymeleafTemplate = thymeleaftemplateDao.findByTemplate(template);
            if (thymeleafTemplate != null) {
                return new ByteArrayInputStream(thymeleafTemplate.getContent().getBytes());
            }
            return null;
        }

        @Override
        public String getName() {
            return "dbResourceResolver";
        }
    }
}

Any help is appreciated...

like image 621
kasdega Avatar asked Apr 24 '17 22:04

kasdega


1 Answers

Through mostly trial and error I was able to piece this together. Posting it here to help the next person looking for something similar.

This is made easier in the newer version of Thymeleaf. All one needs to do now is to extend StringTemplateResolver.

import java.util.Map;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import org.thymeleaf.templateresource.ITemplateResource;

import com.google.common.collect.Sets;

public class ThymeleafDatabaseResourceResolver extends StringTemplateResolver {
    private final static String PREFIX = "";

    @Autowired ThymeleafTemplateDao thymeleaftemplateDao;

    public ThymeleafDatabaseResourceResolver() {
        setResolvablePatterns(Sets.newHashSet(PREFIX + "*"));
    }

    @Override
    protected ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) {

        // ThymeleafTemplate is our internal object that contains the content.  
        // You should change this to match you're set up.

        ThymeleafTemplateDao thymeleaftemplateDao = ApplicationContextProvider.getApplicationContext().getBean(ThymeleafTemplateDao.class);
        ThymeleafTemplate thymeleafTemplate = thymeleaftemplateDao.findByTemplateName(template);  
        if (thymeleafTemplate != null) {
            return super.computeTemplateResource(configuration, ownerTemplate, thymeleafTemplate.getContent(), templateResolutionAttributes);
        }
        return null;
    }

}
like image 74
kasdega Avatar answered Sep 25 '22 12:09

kasdega