在多模块项目中配置Thymeleaf

时间:2020-03-17 17:30:18

标签: java spring-boot module properties thymeleaf

我的项目结构,所以根有2个在pom.xml中声明的模块

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<modules>
    <module>svpm-service</module>
    <module>svpm-web</module>
</modules>

<groupId>md.svpm</groupId>
<artifactId>svpm</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>svpm</name>

enter image description here

Thymeleaf如何无法将路径和配置获取到我的模板文件夹?我有一个多模块项目,其中前端是一个单独的模块,试图至少从放置在根目录中的 WebConfig.java 使其正常工作,但即使如此无法获取路径。

@Bean
public SpringResourceTemplateResolver templateResolver() {
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(this.applicationContext);
    templateResolver.setPrefix("classpath:/templates/"); // < - here the code
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCacheable(true);
    return templateResolver;
}
Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

第一种情况:,即使我将 WebConfig.java 中的路径更改为另一个路径-仍然会显示

找不到模板位置:classpath:/ templates /(请添加一些模板或检查您的Thymeleaf配置)

第二种情况::如果我将以下各行添加到所有 application.properties 中,则仅考虑 svpm-service 中的一行还是没事

spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

请指向正确的方向。

1 个答案:

答案 0 :(得分:0)

问题在于指定模块以及Spring Boot读取特定模块的配置的方式,这很容易理解:

在根pom.xml中,我具有3个模块的followinf声明:

<modules>
    <module>application</module>
    <module>frontend</module>
    <module>service</module>
</modules>

在应用程序的pom.xml中,我只有 service 模块的依赖项

<dependency>
     <groupId>md.svpm</groupId>
     <artifactId>service</artifactId>
     <version>${project.version}</version>
     <type>jar</type>
</dependency>

接下来,在 service 中,我具有 frontend 依赖项

<dependency>
    <groupId>md.svpm</groupId>
    <artifactId>frontend</artifactId>
    <version>${project.version}</version>
    <type>jar</type>
</dependency>

因此,服务了解前端,而应用程序也了解服务,这是设计良好的模块应用程序链。它不会以其他方式起作用。