简体   繁体   English

无法使用 thymeleaf 运行 spring boot 应用程序

[英]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/我的welcome.html 文件:位于/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 :我的 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.仍然无法显示welcome.html。 Any idea regarding ThymeleafAutoconfiguration ?关于ThymeleafAutoconfiguration 的任何想法?

I know this is 3 years later after the first answer.我知道这是在第一个答案之后的 3 年。 But @xingbin answer really helped me a lot.但是@xingbin 的回答真的帮了我很多。

Running the project through mvn clean and mvn spring-boot:run sure works.通过mvn cleanmvn spring-boot:run肯定有效。

But adding the code above to the classpath in application.properties didnt work to me.但是将上面的代码添加到 application.properties 中的类路径对我不起作用。 So I figured it out, I just need to remove the所以我想通了,我只需要删除

Asterisk * in spring.thymeleaf.prefix=classpath*:/templates/ 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.我认为这是因为您的IDE 无法解析您的类路径。

IntelliJ IDEA orders the classpath differently depending on how you run your application. IntelliJ IDEA 根据您运行应用程序的方式对类路径进行不同排序。 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.通过 main 方法在 IDE 中运行应用程序将导致与使用 Maven 或 Gradle 或从其打包的 jar 运行应用程序时不同的顺序。 This can cause Spring Boot to fail to find the templates on the classpath.这可能会导致 Spring Boot 无法在类路径上找到模板。 If you're affected by this problem you can reorder the classpath in the IDE to place the module's classes and resources first.如果您受到此问题的影响,您可以在 IDE 中重新排序类路径以首先放置模块的类和资源。 Alternatively, you can configure the template prefix to search every templates directory on the classpath: classpath*:/templates/.或者,您可以配置模板前缀来搜索类路径上的每个模板目录: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:将资源文件夹添加到类路径,请参阅,然后在文件夹资源下创建一个名为 application.properties 的文件,将下面的配置放入其中以包含所有类路径:

     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 .通过执行mvn cleanmvn spring-boot:run

By default Spring Boot looking for thymeleaf in resources/templates folder.默认情况下,Spring Boot 在资源/模板文件夹中寻找百里香叶。 Just add templates folder to your resource and put you html files in templates只需将模板文件夹添加到您的资源并将您的 html 文件放入模板中

在此处输入图片说明 . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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