2

My main application class :

package com.sopra.springBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {

public static void main(String[] args){
    SpringApplication.run(Application.class, args);
}
}

My controller :

    package com.sopra.springBoot;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String viewHome() {
        return "welcome";
    }
}

My welcome.html file : which is located at /resources/templates/

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<body>First Spring Boot application

</body>

</html>

My pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sopra.springBoot</groupId>
  <artifactId>sopra.springBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
    <relativePath/> 
</parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <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>

</dependencies>

<build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

I tried everything available on internet but still unable to locate templates folder and getting this error and Whitelabel Error Page on browser :

2018-01-23 16:08:38.878  WARN 2904 --- [  restartedMain] o.s.b.a.t.ThymeleafAutoConfiguration     : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

Please check where am i doing mistake. Still not able to display welcome.html. Any idea regarding ThymeleafAutoconfiguration ?

ben3000
  • 4,629
  • 6
  • 24
  • 43
R.Sehdev
  • 411
  • 4
  • 11

3 Answers3

2

I know this is 3 years later after the first answer. But @xingbin answer really helped me a lot.

Running the project through mvn clean and mvn spring-boot:run sure works.

But adding the code above to the classpath in application.properties didnt work to me. So I figured it out, I just need to remove the

Asterisk * in spring.thymeleaf.prefix=classpath*:/templates/

Here is my code :

spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
1

Your code works fine on my computer. I think this is because your IDE can not resolve your classpath.

IntelliJ IDEA orders the classpath differently depending on how you run your application. Running your application in the IDE via its main method will result in a different ordering to when you run your application using Maven or Gradle or from its packaged jar. This can cause Spring Boot to fail to find the templates on the classpath. If you’re affected by this problem you can reorder the classpath in the IDE to place the module’s classes and resources first. Alternatively, you can configure the template prefix to search every templates directory on the classpath: classpath*:/templates/.

This can be solved by

  • adding the resources folder to the class path, see this, then creating a file with name application.properties under folder resources, put below config in it to include all class paths:

    spring.thymeleaf.prefix=classpath*:/templates/
    spring.thymeleaf.check-template-location=true
    spring.thymeleaf.suffix=.html
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.content-type=text/html
    spring.thymeleaf.mode=HTML5
    

Or

  • running the project by executing mvn clean and mvn spring-boot:run.
ben3000
  • 4,629
  • 6
  • 24
  • 43
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • any idea about eclipse oxygen ? I mean how to set classpath in that – R.Sehdev Jan 30 '18 at 12:04
  • @R.Sehdev try this https://www.techwalla.com/articles/how-to-set-the-classpath-in-eclipse , https://stackoverflow.com/questions/25162773/how-do-i-add-a-directory-to-the-eclipse-classpath – xingbin Jan 30 '18 at 12:06
  • 1
    This deserves to be the accepted answer, it was the cause of the problem for me. – ben3000 May 01 '18 at 05:05
0

By default Spring Boot looking for thymeleaf in resources/templates folder. Just add templates folder to your resource and put you html files in templates

enter image description here.

Den B
  • 843
  • 1
  • 10
  • 26