【问题标题】:Unable to run spring boot application with thymeleaf无法使用 thymeleaf 运行 Spring Boot 应用程序
【发布时间】:2018-07-02 03:46:09
【问题描述】:

我的主要应用类:

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);
}
}

我的控制器:

    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";
    }
}

我的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>

我的 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>

我尝试了互联网上可用的所有内容,但仍然无法找到模板文件夹并在浏览器上收到此错误和 Whitelabel 错误页面

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)

请检查我在哪里做错了。仍然无法显示welcome.html。 关于 ThymeleafAutoconfiguration 的任何想法?

【问题讨论】:

  • 您是否仔细检查过welcome.html 文件是否位于src/main/resources/templates 中?

标签: java maven spring-boot thymeleaf


【解决方案1】:

您的代码在我的电脑上运行良好。我想这是因为你的IDE can not resolve your classpath.

IntelliJ IDEA 对类路径的排序取决于您的方式 运行您的应用程序。通过主程序在 IDE 中运行您的应用程序 方法将导致与运行时不同的顺序 使用 Maven 或 Gradle 或其打包的 jar 的应用程序。这个可以 导致 Spring Boot 无法在类路径中找到模板。如果 你受到这个问题的影响,你可以在 IDE 首先放置模块的类和资源。或者, 您可以配置模板前缀以搜索每个模板 类路径上的目录:classpath*:/templates/。

这个可以解决

  • 将资源文件夹添加到类路径中,参见this,然后在文件夹资源下创建一个名为 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
    

或者

  • 通过执行mvn cleanmvn spring-boot:run 运行项目。

【讨论】:

【解决方案2】:

默认情况下,Spring Boot 在资源/模板文件夹中寻找百里香。 只需将模板文件夹添加到您的资源并将您的 html 文件放入模板中

.

【讨论】:

  • 做到了,但仍然没有选择路径。我认为还缺少其他东西
【解决方案3】:

我知道这是在第一个答案之后的 3 年。不过@xingbin 的回答对我帮助很大。

通过mvn cleanmvn spring-boot:run 运行项目肯定可以。

但是将上面的代码添加到 application.properties 中的类路径对我不起作用。所以我想通了,我只需要删除

星号 * 在spring.thymeleaf.prefix=classpath*:/templates/

这是我的代码:

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

【讨论】:

    猜你喜欢
    • 2020-10-03
    • 2017-12-26
    • 2020-11-06
    • 2020-04-20
    • 2020-02-22
    • 1970-01-01
    • 2018-05-08
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多