Quantcast

How to read a Thymeleaf template from the DB in 3.0.5

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to read a Thymeleaf template from the DB in 3.0.5

michael_wb
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 appriciated!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to read a Thymeleaf template from the DB in 3.0.5

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

import org.springframework.stereotype.Service;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import org.thymeleaf.templateresource.ITemplateResource;

import com.google.common.collect.Sets;

@Service
public class ThymeleafDatabaseResourceResolver extends StringTemplateResolver {
    private final static String PREFIX = "db:";

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

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

        String thymeleafTemplate = // code here to pull the template as a string from DB

        if (thymeleafTemplate != null) {
            return super.computeTemplateResource(configuration, ownerTemplate, thymeleafTemplate, templateResolutionAttributes);
        }
        return null;
    }

}
Loading...