Search code examples
javaspring-bootmodulepropertiesthymeleaf

Configure Thymeleaf in a multi-module project


My project structure, so the root has 2 modules declared in 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

How is this that Thymeleaf won't get the path and configs to my templates folder? I have a multi-module project where the frontend is a separate module, trying to get that working at least from WebConfig.java that is placed in root, but even that it won't get the path.

@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)

First case: even if I change the path in WebConfig.java to another one - it still says

Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

Second case: if I add the lines below to all of the application.properties it considers only the one from svpm-service and still nothing

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

Please point me in the right direction.


Solution

  • The problem was in specifying the modules and the way Spring Boot reads configurations for specific module, it is easy to understand that:

    In root pom.xml I have the followinf declaration of 3 modules:

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

    In application's pom.xml I have only the dependency for service module

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

    Next, in service I have the frontend dependency

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

    So in that way, the service knows about frontend and the application knows about service, it is a chain of well designed module application. It will not work other way.