简体   繁体   中英

Unable to run spring boot application with thymeleaf

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 ?

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

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 .

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

在此处输入图片说明 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM