Spring - 无法解析 MVC“查看”百里香叶

Vad*_*tov 3 spring spring-mvc thymeleaf spring-boot

我得到了一个简单的HomeController.class

package com.example.tacos;

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

@Controller
public class HomeController {

    @GetMapping("/")
    public String home() {
        return "home";
    }
}
Run Code Online (Sandbox Code Playgroud)

我也有一个模板叫做 home.html

<!DOCTYPE HTML>
    <head>
    <title>Getting Started: Serving Web Content</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p>Hey</p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

无论如何,我在浏览器中得到 404,IDE 告诉我无法解析 MVC“视图”,正如您在屏幕上看到的那样

文件夹结构: 文件夹结构

浏览器: 浏览器

小智 6

要解决此问题,您应该添加web app打包到 Web 资源目录中的路径。

  1. Alt + Ctrl + Shift + S(“项目结构”窗口打开)
  2. 转到“刻面”选项卡
  3. 然后单击“网络”选项卡
  4. 将新包添加到“Web 资源目录”

在此输入图像描述


小智 5

嗨,我遇到了同样的问题,我通过删除 Thymeleaf 的版本来修复它,因此 pom 文件将如下所示:

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)