如何在Thymeleaf 3.0.5中从DB读取Thymeleaf模板? [英] How to read a Thymeleaf template from DB in Thymeleaf 3.0.5?

查看:87
本文介绍了如何在Thymeleaf 3.0.5中从DB读取Thymeleaf模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在从Thymeleaf 2.1升级到3.0.5。我们当前的设置(升级前)有许多百万美元模板定义并存储在数据库表中。

当我们尝试升级到3.x时,我们的2.1代码不再有效......好吧我们可以找不到任何关于如何用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?

即使是如何实现org.thymeleaf.templateresolver.StringTemplateResolver的一个很好的例子也可能会把我们推向正确的方向......但我们可以'找不到任何东西。

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.

这就是我们在2.1中使用的:

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...

推荐答案

通过大多数的反复试验,我能够把它拼凑起来。将它发布在这里以帮助下一个寻找类似东西的人。

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

在较新版本的Thymeleaf中,这一点变得更容易了。现在需要做的就是扩展StringTemplateResolver。

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;
    }

}

这篇关于如何在Thymeleaf 3.0.5中从DB读取Thymeleaf模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆