Thymeleaf 无法解析多模块 Spring Boot 项目中的模板
Posted
技术标签:
【中文标题】Thymeleaf 无法解析多模块 Spring Boot 项目中的模板【英文标题】:Thymeleaf can't resolve templates in multimodule SpringBoot project 【发布时间】:2020-05-14 22:05:00 【问题描述】:我遇到了标题中描述的问题。
我创建了具有以下结构的多模块 SpringBoot 项目:
父模块(pom打包) 前端模块(jar 包装) 业务模块(jar 包装) run-module(战争包装)当我直接通过 IntelliJ 运行应用程序时,它工作正常,但是当我通过 Maven 构建包并在控制台创建的 run-module.war 文件中运行时,它会抛出:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [pages/example-page], template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar!/:3.0.11.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) ~[spring-webmvc-5.2.2.RELEASE.jar!/:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118) ~[spring-webmvc-5.2.2.RELEASE.jar!/:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) ~[spring-webmvc-5.2.2.RELEASE.jar!/:5.2.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.2.RELEASE.jar!/:5.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.2.RELEASE.jar!/:5.2.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.2.RELEASE.jar!/:5.2.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.29.jar!/:9.0.29]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.2.RELEASE.jar!/:5.2.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.29.jar!/:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.29.jar!/:9.0.29]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar!/:9.0.29]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.29.jar!/:9.0.29]
...
我可以看到run-module.war里面有一个front-module.jar作为lib。
有人知道如何“告诉”Thymeleaf 模板在哪里吗?
【问题讨论】:
请显示您的任何配置。 @chrylis-onstrike- 我没有任何配置。我尝试添加以下属性:spring.thymeleaf.prefix=/WEB-INF/lib/front-module.jar/templates
spring.thymeleaf.suffix=.html
【参考方案1】:
我通过以下配置解决了这个问题,创建一个ClassLoaderTemplateResolver很重要:
@Configuration
public class ThymeleafConfiguration
@Bean
public ClassLoaderTemplateResolver thymeleafTemplateResolver()
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix("/templates/");
resolver.setSuffix(".html");
resolver.setCacheable(false);
resolver.setCharacterEncoding("UTF-8");
resolver.setTemplateMode(TemplateMode.HTML);
return resolver;
@Bean
public SpringTemplateEngine templateEngine()
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
return templateEngine;
@Bean
public ThymeleafViewResolver thymeleafViewResolver()
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
【讨论】:
【参考方案2】:将协议“类路径”添加到您的前缀中,不带 jar 文件名
spring.thymeleaf.prefix=classpath:/templates/
【讨论】:
感谢您的回答,但不幸的是,这并不能解决问题。以上是关于Thymeleaf 无法解析多模块 Spring Boot 项目中的模板的主要内容,如果未能解决你的问题,请参考以下文章
CSS 样式表不适用于 Spring Security + Spring Boot + Thymeleaf
Thymeleaf 模板无法正确解析我的 html 5 页面
Thymeleaf 3 URL 解析不适用于 Spring Boot 4 + Spring Security
Spring boot - Thymeleaf 模板 - 多个解析器