Gradle 在springboot中找不到Thymeleaf视图

Gradle 在springboot中找不到Thymeleaf视图,gradle,spring-boot,thymeleaf,Gradle,Spring Boot,Thymeleaf,我试图按照本教程将thymeleaf添加到springboot应用程序中,但我似乎无法让它正常工作。 教程: 当我在LoginController中使用@RestController启动应用程序时,我能够让springboot正常工作,但当我将@RestController更改为@Controller时,我收到一个错误页面,上面说: 出现意外错误(类型=未找到,状态=404)。 没有可用的消息 我在控制器中设置了一个断点,并确认它正在命中LoginController中的index方法。我觉得这

我试图按照本教程将thymeleaf添加到springboot应用程序中,但我似乎无法让它正常工作。 教程:

当我在LoginController中使用@RestController启动应用程序时,我能够让springboot正常工作,但当我将@RestController更改为@Controller时,我收到一个错误页面,上面说:

出现意外错误(类型=未找到,状态=404)。 没有可用的消息

我在控制器中设置了一个断点,并确认它正在命中LoginController中的index方法。我觉得这与我添加Thymeleaf的方式有关,因为我没有对应用程序做太多其他操作,但迄今为止我尝试的所有操作都会导致相同的错误页面

我的身材,格雷德尔

buildscript {
repositories {
    maven { url "http://repo.spring.io/libs-snapshot" }
    mavenLocal()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
baseName = 'GazeFest'
version =  '0.1.0'
}

repositories {
mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.thymeleaf:thymeleaf-spring4:3.0.0.RELEASE")
}

task wrapper(type: Wrapper) {
gradleVersion = '3.0'
}
my Application.java

包装博览会

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 LoginController.java

package gazefest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class LoginController {

@RequestMapping("/")
public String index(Model model) {
    model.addAttribute("message", "HELLO!");
    return "index";
}

}
my index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8" />
<title>HELLO</title>
</head>
<body>
<p th:text="${message}"></p>
</body>
</html>

你好

我的文件结构


谢谢你看

我认为您不应该使用thymeleaf-spring4依赖项,但您应该使用thymeleaf的Spring启动程序

对于Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

我建议使用来设置您的项目。这允许您选择任何Spring启动启动程序,并将其添加到Gradle/Maven描述符中,这样您就不会因为选择依赖项而犯任何错误。

我尝试使用上面相同的LoginController.java使用Spring Initializer启动一个新项目,但遇到了相同的错误页面。是否需要指定html的位置文件在哪里?我认为我当前使用index.html的resources/templates文件夹应该是默认的,但它似乎找不到它们src/main/resources/templates文件夹(应该由Spring的初始值ZR生成)应该是正确的文件夹是的。添加compile(“org.springframework.boot:Spring boot starter thymeleaf”)为了解决我的问题。这是一件非常棘手的事情,因为最初我在控制台中看到一个404页面,并且没有错误日志。在这种情况下,问题实际上是缺少thymeleaf依赖性,这一点都不明显。
compile("org.springframework.boot:spring-boot-starter-thymeleaf")