java - Thymeleaf 无法解析多模块 Spring Boot 项目中的模板

标签 java spring-boot maven thymeleaf multi-module

我在标题中描述了问题。

我创建了具有以下结构的多模块 SpringBoot 项目:

  • 父模块(pom 包装)
  • 前置模块( jar 装)
  • 业务模块( jar 装)
  • 运行模块( war 包装)

  • 当我直接通过 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 模板在哪里吗?

    最佳答案

    我通过以下配置解决了这个问题,创建一个 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;
        }
    }
    

    关于java - Thymeleaf 无法解析多模块 Spring Boot 项目中的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59966567/

    相关文章:

    java - HystrixRequestContext.getContextForCurrentThread() 变为 null

    java - Logback 覆盖依赖 logback.xml

    java - 一个简单的客户端服务器套接字程序的PrintWriter和OutputStream

    java - javafx更改swingNode的大小

    java - MockMvc 和 Mockito - java.lang.AssertionError : JSON path "$" Expected: a collection with size <2> but: collection size was <0>

    java - 无法从带有嵌套模块的 IntelliJ maven 项目中删除无效项目

    java - 未正确定义ubuntu java_home上的maven2

    java - 在单元测试中覆盖默认的 Spring @Value 注释值

    java - SOAP 和需要 Java serialVersionUID

    java - 应用程序无法加载 application.yml 文件