Spring boot - Thymeleaf 模板 - 多个解析器

Posted

技术标签:

【中文标题】Spring boot - Thymeleaf 模板 - 多个解析器【英文标题】:Spring boot - Thymeleaf template - multiple resolvers 【发布时间】:2016-01-27 02:58:00 【问题描述】:

我们的项目中有多个 thmeleaf 模板解析器。

我们的项目结构,

/src/main/java/*.java
/src/main/resources/pages/*.html
/src/main/resources/templates/*.html

在里面,resources,我们有 pages 和 templates 文件夹。

因此,必须再添加一个视图解析器才能在页面中包含 html 文件。

@Configuration
public class ThymeleafConfig 

    @Autowired
    private SpringTemplateEngine templateEngine;

    @PostConstruct
    public void init() 
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();

        resolver.setPrefix("pages/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("LEGACYHTML5");
        resolver.setOrder(templateEngine.getTemplateResolvers().size());

        templateEngine.addTemplateResolver(resolver);
    


现在,想将几个文件移动到 /srs/main/webapp/

/src/main/resources/
/src/main/webapp/*.html

所以,尝试了上面的配置文件,

resolver.setPrefix("webapp/");

也试过下面的代码,

 resolver.setPrefix("/webapp/");

现在,出现以下错误。

2015-10-27 15:36:13.078 [http-nio-8080-exec-8] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8080-exec-8] Exception processing template "agent": Error resolving template "agent", template might not exist or might not be accessible by any of the configured Template Resolvers
2015-10-27 15:36:13.094 [http-nio-8080-exec-8] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "agent", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "agent", template might not exist or might not be accessible by any of the configured Template Resolvers
    at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]

对 webapp 文件夹进行的任何其他配置...

【问题讨论】:

【参考方案1】:

将 ClassLoaderTemplateResolver 更改为 ServletContextTemplateResolver 解决了这个问题。我猜,由于安全原因,ClassLoaderTemplateResolver 无法读取 webapp 下的文件。

@PostConstruct
    public void init() 
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();

        resolver.setPrefix("/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("LEGACYHTML5");
        resolver.setOrder(templateEngine.getTemplateResolvers().size());

        templateEngine.addTemplateResolver(resolver);
    

【讨论】:

以上是关于Spring boot - Thymeleaf 模板 - 多个解析器的主要内容,如果未能解决你的问题,请参考以下文章

spring boot整合Thymeleaf

Spring Boot2:使用Spring Boot结合Thymeleaf模板引擎使用总结

Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例

Spring Boot 学习笔记--整合Thymeleaf

Spring Boot入门——thymeleaf模板使用

Spring Boot模板引擎Thymeleaf demo