How to locate Thymeleaf template from spring boot
Asked Answered
F

7

15

I am trying to follow this tutorial at http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html to learn how to send responses to Thymeleaf template. But I get this error: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

I put the message.html file in Other Sources directory and inside src/main/resources under <default package>.

So the structure looks like :

SomeProject -Other Sources --src/main/resources ---<default package> ----message.html

I was wondering why it shows under <default package> but not under <template> ? Could it be the problem? If so how am I supposed to change it? I am using netbeans and maven. Any ideas please? These are the dependencies I have in my pom.xml:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>            
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

In the controller I have

@RequestMapping(value = "message", method = RequestMethod.GET)
    public String messages(Model model) {
    model.addAttribute("messages", messageRepository.findAll());
    return "message";
}

And in the view:

<ul th:each="message : ${messages}">
    <li th:text="${message.id}">1</li>
    <li><a href="#" th:text="${message.title}">Title ...</a></li>
    <li th:text="${message.text}">Text ...</li>
</ul>   
Featurelength answered 25/12, 2016 at 1:3 Comment(0)
S
5

2 things here : 1. If you are using Maven, and I assume no customizations to folder names. Then the folder name should be src instead of source. 2. Once the folder has been renamed move your templates into 'templates' folder inside src/resources this should run fine.

Sirius answered 25/12, 2016 at 1:45 Comment(4)
@Hmanshu Bhardwaj I made a typo in the question. In netbeans the folder structure shows like this src/main/resources which is under the Other resources directory in netbeans project hierarchy. I will fix the typo in the question. Thanks for your help. Now maybe you can give some further feedback?Featurelength
I already gave 2 points. Are your templates in templates directory under src/main/resources?Sirius
@ Himanshu I created the templates directory under src/main/resources and put the html file there and now the problem is resolved. Thanks for your support. I will mark the question as answered.Featurelength
For me the only mistake was the name of the folder. Just make sure to write it correctly templatesEvonevonne
E
25

Spring Boot includes auto-configuration support for the thymeleaf templating engines, your templates will be picked up automatically from src/main/resources/templates.

if you are customize you template location then use below thymeleaf property configuration available in Spring Boot.

 spring.thymeleaf.check-template=true # Check that the template exists before rendering it.

 spring.thymeleaf.check-template-location=true # Check that the templates location exists.

 spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.

 spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.

 spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
Epideictic answered 25/12, 2016 at 6:39 Comment(3)
From where I am supposed to find this configuration file? I am using maven and when I try to change some from the dependency lists it does not allow me to change anything there. Some codes are even being hidden. Am I supposed to download the dependencies manually for which I want to change configurations?Featurelength
Add a application.properties file in your class path and put all the properties. src/main/resources/application.propertiesEpideictic
this is not workingZuniga
L
6

The default directory location for thymeleaf templates is:

src/main/resources/templates

The other paths are standard conventions for Spring Boot.

Spring Boot thymeleaf directory structure

Lett answered 28/5, 2017 at 10:55 Comment(0)
S
5

2 things here : 1. If you are using Maven, and I assume no customizations to folder names. Then the folder name should be src instead of source. 2. Once the folder has been renamed move your templates into 'templates' folder inside src/resources this should run fine.

Sirius answered 25/12, 2016 at 1:45 Comment(4)
@Hmanshu Bhardwaj I made a typo in the question. In netbeans the folder structure shows like this src/main/resources which is under the Other resources directory in netbeans project hierarchy. I will fix the typo in the question. Thanks for your help. Now maybe you can give some further feedback?Featurelength
I already gave 2 points. Are your templates in templates directory under src/main/resources?Sirius
@ Himanshu I created the templates directory under src/main/resources and put the html file there and now the problem is resolved. Thanks for your support. I will mark the question as answered.Featurelength
For me the only mistake was the name of the folder. Just make sure to write it correctly templatesEvonevonne
R
2

For Thymeleaf template files put in folder src/main/resources/templates/ and it will work for you also do check out http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf/ . Hope you will find it easy to kick start thymeleaf with spring boot.

Rosalba answered 28/5, 2017 at 15:28 Comment(0)
O
1

I had the same problem with Spring boot + Gradle and these are the steps that I followed to resolve it:

  1. Print out the classpath Classpath Print out

  2. Once I established that the resources folder was not being included in the classpath, I reloaded the application from disk:

Gradle Reload

  1. Gradle clean the project Gradle clean task
Omnivorous answered 13/9, 2020 at 10:20 Comment(0)
B
1

I see a lot of issues regarding the ThymeLeaf /templates not found problem. For everyone using Eclipse or STS the solution is maybe simpler then any type of configuration. In the course of the project operation with maven, that is compile, clean, build and others, the final deployment Output Directory on your eclipse workspace (or staging area)

[eclipseprojectrootfolder]/target/

can erase some of the output directory structure and not re-created them it properly.

In my case I went for all sorts of solutions on application.properties, changing the autoconfig on the Spring app to no avail. The solution was simple: Just look inside

[eclipseprojectrootfolder]/target/classes

and see if in that directory you can find the directories

[eclipseprojectrootfolder]/target/classes/templates/

and

[eclipseprojectrootfolder]/target/classes/static/

Those are the folders inside the resources directory:

 [eclipseprojectrootfolder]/src/main/resources

simply Copy the folders inside [eclipseprojectrootfolder]/src/main/resources, that is the

/templates 
/static

into [eclipseprojectrootfolder]/target/classes That is the Solution. Spring is complaining exactly because of that ..can not find /templates ... because the directory on the Output deployment folder/directory may indeed not be there ... Just recreate the directory manually and all will be fine. Important note, Of course when I mention that a manual copy of the directories may be necessary this means we need to recreate the directories And the files inside that are the templates and static content for thymeleaf.

Bedlamite answered 12/6, 2022 at 17:24 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Octennial
C
0

create new folder by right clicking on you project - name it templates - copy all old templates files to this new one.

if you are still getting this problem then you need to update maven by selecting help and then workplace.

Chiba answered 21/3, 2023 at 4:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.