Spring MVC 如何在Thymeleaf 3.0.5中从DB读取Thymeleaf模板?

8aqjt8rx  于 2023-10-24  发布在  Spring
关注(0)|答案(1)|浏览(219)

我们正在从Thymeleaf 2.1升级到3.0.5。我们当前的设置(升级前)有许多定义的Thymeleaf模板,并存储在数据库表中。
当我们试图升级到3.x时,我们的2.1代码不再工作.好吧,但我们找不到任何好的例子来说明如何用Thymeleaf 3.0.5做基本相同的事情。有人实现了吗?
即使是一个如何实现org.thymeleaf.templateresolver.StringTemplateResolver的好例子也可能会把我们推向正确的方向.
这是我们在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";
        }
    }
}

任何帮助都很感激…

pieyvz9o

pieyvz9o1#

通过大部分的尝试和错误,我能够拼凑起来。张贴在这里,以帮助下一个人寻找类似的东西。
这在新版本的Thymeleaf中变得更容易了。现在需要做的就是扩展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;
    }

}

相关问题